IT story/JSP

[Jsp 강의] L05Redirect

jason719 2016. 10. 27. 15:57

L05Redirect

로그인 과정을 통해 Redirect 기능을 알아보자

index.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%-- index.jsp를 호출하면  redirect로 스쳐지나가게 할 예정이다. --%>
<%
    System.out.print("index.jsp 페이지를 지나쳤다~");
    response.sendRedirect("./L01LoginForm.jsp");
%>
</body>
</html>
cs


예제1 L01LoginForm.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>servlet을 이용해서 로그인을 성공하자</title>
</head>
<body>
    <h1>로그인 form</h1>
    <%-- 제출한 파라미터와 db에서 가져온 id와 pw를 L02LoginController.java에서 검사 
        같으면 true를 파라미터로 L03LoginResult.jsp로 보낸다. -> 성공 메시지
        실패하면 L01LoginForm.jsp 페이지로 와서 id와 pwd가 틀렸습니다. 메시지 출력 
        
         L03LoginResult.jsp에서 파라미터 true가 없으면 접근할 수 없도록해라
         (강제로 L01LoginForm.jsp)로 이동
    --%>
    <%     String login = request.getParameter("msg");
        if(login != null){
    %>
        <h3 style="color: red"><%=login %></h3>
    <%        
        }
    %>
    
    <form action="./loginCtrl" method="post">
        <p>
            <label>아이디:</label>
            <input type="text" name="id" value="jsp">
        </p>
        <p>
            <label>비밀번호:</label>
            <input type="password" name="pwd" value="admin1234">
        </p>
        <button type="submit">제출</button>
    </form>
    
</body>
</html>
cs


예제2 L02SignupController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.jsp.controller;
 
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
@WebServlet("/loginCtrl")
public class L02SignupController extends HttpServlet {
    private static final long serialVersionUID = 1L;
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
    }
 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String db_id = "jsp";
        String db_pwd= "admin1234";
        
        
        response.setContentType("text/html; charset=utf-8");
        String id = request.getParameter("id");
        String pwd = request.getParameter("pwd");
        System.out.println("id: "+id+" //pwd: "+pwd);
        if(id.equals(db_id) && pwd.equals(db_pwd)){
            response.sendRedirect("./L03LoginResult.jsp?login=1");
            //login이 1이면 true, 0이면 false
 
        }else{
            response.sendRedirect("./L01LoginForm.jsp?msg=login failed");
        }
    }
 
}
 
cs
L


L03LoginResult.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>login 성공 페이지</title>
</head>
<body>
    <%-- parameter login이 오지 않으면 이 페이지에 접근할 수 없다.
          이때 login은 세션 또는 쿠키 대신이다.
          세션 or 쿠키는 브라우저가 유지하고 있는 파라미터이다. --%>
          <%
          String login = request.getParameter("login");
          boolean access = false;
          int int_login = 0;
          if(login != null){
              int_login = Integer.parseInt(login);
              if(int_login == 1){
                  access = true;
              }
          }
          if(!access){
              response.sendRedirect("./L01LoginForm.jsp?msg=access failed");
          }
          %>
    <h1>로그인 성공 페이지</h1>
    
</body>
</html>
cs


'IT story > JSP' 카테고리의 다른 글

[Jsp 강의] L07Template  (0) 2016.10.27
[Jsp 강의] L06Forward  (0) 2016.10.27
[Jsp 강의] L04JSP  (0) 2016.10.27
[Jsp 강의] L03ServletMethod  (0) 2016.10.27
[Jsp 강의] L02HelloJsp  (0) 2016.10.27