본문 바로가기
☕Java/Spring

[20210708] 스프링을 이용한 로그인, 회원가입

by 캔 2021. 7. 8.
<!-- step1.jsp -->

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
	$(function(){	
		$("#btn").on('click',function(){
			document.frm.action="step2.do";
			document.frm.method="post";
			document.frm.submit();
		});		
	});
</script>
</head>
<body>
	<h2>step1.jsp</h2>
	<h2>약관</h2>
	<textarea name="txtarea" id="txtarea" rows="10" cols="30">
		당신의 소중한 개인정보는 제가 잘 써보겠습니다.. ㅋㅋㅋ</textarea>
	<form name="frm" action="">
		<input type="checkbox" name="ck" id="agree" />
		<span>약관동의</span>
		<input type="button" value="다음" id="btn" />
	</form>		
</body>
</html>​
<!--maven pom.xml-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>spring_web05</groupId>
  <artifactId>spring_web05</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <dependencies>
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-context</artifactId>
	    <version>4.3.30.RELEASE</version>
	</dependency>
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-webmvc</artifactId>
	    <version>4.3.30.RELEASE</version>
	</dependency>
	<dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
	</dependency>
  </dependencies>  
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <release>15</release>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.3</version>
      </plugin>
    </plugins>
  </build>
</project>
<!--web.xml-->
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>spring_web09</display-name>
  <servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/spring-app.xml
						/WEB-INF/spring-controller.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>		
	</servlet-mapping>  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

<!--spring-app.xml-->

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<!-- 이름에 따라 알아서 처리하라고 스프링에 요청 -->
<mvc:annotation-driven />
<!-- 스프링이 처리하지 않는 명령을 처리 ex) 이미지 처리... -->
<mvc:default-servlet-handler/>
<!-- 요청 경로와 뷰 이름을 연결하면 이 방법으로 설정 가능. -->
<mvc:view-controller path="/main" view-name="main"/>
<mvc:view-resolvers>
	<mvc:jsp prefix="/views/" suffix=".jsp"/>
</mvc:view-resolvers>
</beans>

 

 

<!-- spring-controller.xml -->

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

<mvc:view-resolvers>
	<mvc:jsp prefix="/views/" suffix=".jsp"/>
</mvc:view-resolvers>

<context:component-scan base-package="spring_web09"></context:component-scan>
</beans>
//LoginController.java

package spring_web09.control;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class LoginController {
	@RequestMapping(value="/member/login.do", method=RequestMethod.GET)
	public String login(){
		
		return "login/login";
	}
}

 

 

//RegisterController.java

package spring_web09.control;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import spring_web09.dto.MemberDTO;

@Controller
public class RegisterController {
	@RequestMapping("/register/step1.do")
	public String processStep1(){
		
		return "step1";
	}
	
	@RequestMapping("/register/step2.do")
	public String processStep2(HttpServletRequest req){
		
		String ck = req.getParameter("ck");
		if(ck==null){
			return "step1";
		}else {
			return "step2";
		}			
	}

	@RequestMapping("/register/step3.do")
	public String processStep3
	(@ModelAttribute()MemberDTO dto, Model model){
		model.addAttribute("dto", dto);
		return "welcome";
	}
	
	@RequestMapping("/main")
	public String processStep4(){
		return "main";
	}
	
	
}

 

//MemberDTO.java

package spring_web09.dto;

public class MemberDTO {

	private String id;
	private String pwd;
	private String confirm;
	private String email;
	
	public MemberDTO() {}
	
	public MemberDTO(String id, String pwd, String confirm, String email) {
		super();
		this.id = id;
		this.pwd = pwd;
		this.confirm = confirm;
		this.email = email;				
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String getConfirm() {
		return confirm;
	}

	public void setConfirm(String confirm) {
		this.confirm = confirm;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}
	
	
}

 

<!-- main.jsp -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h2>main.jsp</h2>
	<img src="<c:url value='/images/spring.jpg' />" alt="스프링" />
</body>
</html>

 

<!-- step2.jsp -->

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
	$(function(){
		$("#btn").on('click',function(){
			$("#id").val();
			$("#pwd").val();
			$("#confrim").val();
			$("#email").val();
			document.frm.action='<c:url value="/register/step3.do"></c:url>';
			document.frm.method="get";
			document.frm.submit();
		});
	});
</script>
</head>
<body>
	<h2>step2.jsp</h2>
	<form name="frm">
		<table>
			<tr>
				<th>ID</th>
				<td><input type="text" name="id" id="id" /></td> 
			</tr>
			<tr>
				<th>PW</th>
				<td><input type="password" name="pwd" id="pwd" /></td> 
			</tr>
			<tr>
				<th>Comfirm</th>
				<td><input type="password" name="confirm" id="confirm" /></td> 
			</tr>
			<tr>
				<th>Email</th>
				<td><input type="email" name="email" id="email" /></td> 
			</tr>
			<tr>
				<td colspan="2">
					<input type="button" value="가입" id="btn" />
					<input type="button" value="돌아가기" onclick="javascript:history.back()" />
				</td>
			</tr>
		</table>
	</form>
</body>
</html>

 

 

<!-- welcome.jsp -->

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
	$(function(){
		setTimeout(callback, 10000);
	});
	
	function callback(){
		location.href='<c:url value="/member/login.do"></c:url>';
	}  
</script>


</head>
<body>
	<h2>welcome.jsp</h2>
	<h3>가입해주셔서 감사합니다.</h3>
	<h4>가입정보 출력
	id : ${dto.id }
	pw : ${dto.pwd }
	confirm : ${dto.confirm }
	email :	${dto.email }
	</h4>
	
	
	<h4>10초 후에 이동합니다</h4>
	<a href="../main">[첫 화면으로 이동]</a>
</body>
</html>