본문 바로가기

JSP/기본다지기

JSP 6일차 필기 (Session을 이용한 로그인)

index.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Session을 알아보자</title>
</head>
<body>
    <h1>Session 수업</h1>
    <h3><a href="./L01SetSession.jsp">Session 세팅</a></h3>
    <h3><a href="./L02GetSession.jsp">Session 값 얻어오기</a></h3>
    <h3><a href="./L03GetSessions.jsp">Session 모든 값 얻어오기</a></h3>
    <h3><a href="./L04InfoSession.jsp">Session 정보</a></h3>
    <h3><a href="./L05RemoveSession.jsp">Session 삭제</a></h3>
    <h3><a href="./login/L01LoginForm.jsp">Session을 이용한 로그인</a></h3>
</body>
</html>
cs


login폴더 내부에

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
40
41
42
43
44
45
46
47
48
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<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에서 검사
    같으면 login 세션을 생성후 1을 대입 and id 세션 생성, 다르면 0을 대입.
    
    이 때 L03LoginResultjsp redirect 방식으로 이동해라.
    id는 session으로 출력
    
    L03LoginResult.jsp에서 session login이 없거나 0이면 접근할 수 없도록 해라.
    (강제로 L01LoginForm.jsp)로 이동
    
    L03LoginResult.jsp에 로그아웃 버튼 -> L04Logout.java(/logout)
    -->
    <%
        Object login = session.getAttribute("login");
        String msg = request.getParameter("msg");
        if (login != null) {
            if ((int) login == 0) {
    %>
    <h3 style='color: red'>아이디 또는 패스워드를 잘못 입력했습니다.</h3>
    <hr>
    <%
        session.removeAttribute("login");
                login = null;
            }
        }
    %>
    <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


L03LoginResult.sjp

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
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>로그인 성공</title>
</head>
<%
Object login = session.getAttribute("login");
boolean access = false;;
if(login != null){
    if((int)login == 1){
        access = true;
    }
}
%>
<body>
<!-- 
    parameter login이 오지 않으면 이 페이지에 접근할 수 없다.
    이 때 login은 세션 또는 쿠키 대신이다.
    세션 or 쿠키는 브라우저가 유지하고 있는 파라미터이다.
 -->
<%if(access){%>
    <h1><%=session.getAttribute("id")%> 로그인 성공</h1>
    <a href="./../Logout">
        <button>로그아웃</button>
    </a>
<%}else{response.sendRedirect("./L01LoginForm.jsp?msg=Access Denied.");}%>
</body>
</html>
cs


src/com.jsp.login 안에

L02LoginController 서블릿

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
package com.jsp.login;
 
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;
import javax.servlet.http.HttpSession;
 
@WebServlet("/LoginCtrl")
public class L02LoginController extends HttpServlet {
    private static final long serialVersionUID = 1L;
 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        HttpSession session = request.getSession();
        String id = request.getParameter("id");
        String pwd = request.getParameter("pwd");
        String db_id = "jsp";
        String db_pwd = "admin1234";
        if(id!=null && pwd!=null){
            if(id.equals(db_id)&&pwd.equals(db_pwd)){
                session.setAttribute("login"1);
                session.setAttribute("id", id);
                response.sendRedirect("./login/L03LoginResult.jsp");
            }else{
                session.setAttribute("login"0);
                response.sendRedirect("./login/L01LoginForm.jsp");
            }
        }
    }
}
 
cs


L04LogoutController 서블릿

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.jsp.login;
 
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("/Logout")
public class L04LogoutController extends HttpServlet {
    private static final long serialVersionUID = 1L; 
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getSession().invalidate();//세션 만료
        response.sendRedirect("./login/L01LoginForm.jsp?");
    }
}
 
cs


'JSP > 기본다지기' 카테고리의 다른 글

JSP 7일차 필기 (Bean)  (0) 2016.10.26
JSP 7일차 필기 (Cookie)  (0) 2016.10.26
JSP 6일차 필기 (Session)  (0) 2016.10.25
JSP 6일차 필기 (Template)  (0) 2016.10.25
JSP 5일차 필기 (Template)  (0) 2016.10.24