20.03 ~ 20.08 국비교육/JSP

쿠키 사용 로그인 예제

찹키리 2020. 4. 29. 17:06

<쿠키를 사용한 로그인/로그아웃 스토리>

 

 

 

 

 

1) 쿠키가 있는 지 체크

2)T 로그인 된 상태의 뷰 출력 
3)F 로그인 폼 페이지 출력 
4)유효성 검사

 

--- 전송 --->

 

0. 한글 인코딩
1. 받는다.

2. 컨트롤 처리(import, 인스턴스화, 메소드 호출)
3. id, pwd가 존재하는 지 확인(DB 회원 테이블)
4. T 로그인 인증
5. 쿠키 발급
6. 메시지 출력, 결과페이지 이동

4-1. F 경고 메시지 출력
4-2. 다시 뷰 페이지로 돌아감

 

<--- T/F 응답---

7. 결과 뷰 페이지로 전송

 

 

 

 

 

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
import java.sql.*;
 
public class RegisterMgr {
    
    private DBConnectionMgr pool;
    
    public RegisterMgr() {
        try {
            pool = DBConnectionMgr.getInstance();
        } catch(Exception e) {
            System.out.println("Error: 커넥션 연결 실패");
        }
    }
    
    public boolean loginRegister(String id, String pwd) {
        Connection con = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        boolean loginCon = false;
        
        try {
            con = pool.getConnection();
            String query = "select count(*) from tblRegister where id = ? and pwd = ?";
            
            stmt = con.prepareStatement(query);
            stmt.setString(1, id);
            stmt.setString(2, pwd);
            rs = stmt.executeQuery();
            
            if(rs.next() && rs.getInt(1> 0) {
                loginCon = true;
            }
        } catch (Exception ex) {
            System.out.println("Exception" + ex);
        } finally {
            pool.freeConnection(con, stmt, rs);
        }
        
        return loginCon;
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

 

DAO(Java 페이지)

: 데이터베이스 연동(Connection pool), 실제 회원 정보가 존재하는 지 아닌 지 확인하는 쿼리를 먼저 작성했다.(순서 상 로그인 처리 뒤에 실행됨)

 

 

 

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
49
50
51
52
53
54
55
<%
     String cookieName = "";
     String id = "";
     Cookie[] cookies = request.getCookies();
     
     if(cookies != null) {
         for(int i = 0; i < cookies.length; i++) {
             if(cookies[i].getName().equals("idkey")) {
                 cookieName = cookies[i].getName();
                 id = cookies[i].getValue();
             }
         }
         
         if(!id.equals("")) {
%>
<script>
    alert("로그인 되었습니다.");
    location.href = "cookieLoginOK.jsp";
</script>
<%
         }
     }
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Cookie 로그인</title>
</head>
<body>
    <h2 align = "center">Cookie 로그인</h2>
    <form method = "post" action = "cookieLoginProc.jsp">
    <table width = "300" border = "1" align = "center" bgcolor = "#FFFF99">
    <tr bgcolor = "#FFFF66">
    <td colspan = "2" align = "center"><b>Log in</b></td>
    </tr>
    
    <tr>
    <td width = "47%" align = "center">ID</td>
    <td width = "53%" align = "center"><input name = "id"></td>
    </tr>
    
    <tr>
    <td align = "center">PWD</td>
    <td align = "center"><input name = "pwd"></td>
    </tr>
    
    <tr>
    <td colspan = "2" align = "center">
    <input type = "submit" value = "login">
    <input type = "reset" value = "reset">
    </td>
    </tr>
    </table>
    </form>
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

 

로그인 폼 페이지

: 먼저 로그인이 되어 있는 지, 로그인 되지 않은 상태인 지 쿠키체크

로그인이 되어있으면 자바스크립트 실행, 쿠키가 없으면 body 실행

 

 

 

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
<jsp:useBean id="regMgr" class = "chapter12.RegisterMgr"></jsp:useBean>
<%
    String id = "";
    String pwd = "";
    if(request.getParameter("id"!= null)
        id = request.getParameter("id");
    if(request.getParameter("pwd"!= null)
        pwd = request.getParameter("pwd");
    if(regMgr.loginRegister(id, pwd)) {
        Cookie cookie = new Cookie("idkey", id);
        
        response.addCookie(cookie);
%>
 
<script>
    alert("로그인 되었습니다.");
    location.href = "cookieLoginOK.jsp";
</script>
 
<% } else { %>
 
<script>
    alert("로그인이 되지 않았습니다.");
    location.href = "cookieLogin.jsp";
</script>
 
<% }  %>
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

 

로그인 처리 페이지

: 데이터 베이스에서 아이디, 패스워드 존재 여부를 확인하고, 존재하는 경우에 로그인을 실행하는 메소드

 

 

 

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
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
    String id = "";
    Cookie[] cookies = request.getCookies();
    if(cookies != null) {
        for(int i = 0; i < cookies.length; i++) {
            if(cookies[i].getName().equals("idkey")) {
                id = cookies[i].getValue();
            }
        }
        
        if(id.equals("")) {
%>
 
<script>
    alert("로그인 되지 않았습니다.")
    location.href = "cookieLogin.jsp";
</script>
 
<%
        }
    }
 
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Cookie 로그인</title>
</head>
<body>
    <h2 align = "center">Cookie 로그인</h2>
    <table width = "300" border = "1" align = "center" bgcolor = "#FFFF99">
    <tr bordercolor = "FFFF66">
    <td colspan = "2" align = "center"><b>Log On Page</b></td>
    </tr>
    
    <tr>
    <td align = "center"><b><%= id %></b>님이 로그인 하셨습니다.</td>
    <td align = "center"><a href = "cookieLogout.jsp">로그아웃</a></td>
    </tr>
    </table>
</body>
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

 

Java에서 리턴받은 결과 값

T: 뷰 처리

F: 경고 메시지 - 로그인 페이지 이동

 

 

 

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"%>
<%
    Cookie[] cookies = request.getCookies();
    if(cookies != null) {
        for(int i = 0; i < cookies.length; i++)    {
            if(cookies[i].getName().equals("idkey")) {
                cookies[i].setMaxAge(0);
                
                response.addCookie(cookies[i]);
            }
        }
    }
%>
<script>
    alert("로그아웃 되었습니다");
    location.href = "cookieLogin.jsp"
</script>
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

 

로그아웃 페이지

 

 

 

 

-실행

 

 

로그인 폼

 

 

 

로그인 처리 jsp - Java(비즈니스 메소드) - Connection pool 연동 객체 - SQL 요청 - T 반환

 

 

 

 

로그인 성공 페이지

 

 

 

 

로그아웃 페이지

 

 

 

 

아이디와 패스워드를 잘못 입력한 경우

 

 

 

 

로그인 처리 jsp - Java(비즈니스 메소드) - Connection pool 연동 객체 - SQL 요청 - F 반환 - 경고 메시지 - 이동