Coding Note

[JSP] JDBC 연동 - DB 로그인, 로그아웃하기_2 본문

Web/Jsp

[JSP] JDBC 연동 - DB 로그인, 로그아웃하기_2

jinnkim 2022. 2. 6. 11:00

이어서!

 

 

구현

 

 

 

코드

 

5.  login.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>loginForm</title>
<style type="text/css">
        .wrap {
        padding: 30px;    
        width: 300px;
        height: 180px;
        border: 5px solid #CEE3F6;
        text-align: center;
    }
    .id {
        margin-left: 16px;
    }
    .btn {
        outline: none;
        border-radius: 3px;
        background-color: #CEE3F6;    
        margin-top: 16px;
        float: right;
    }
</style>
</head>
<body>
<div class="wrap">
    <h2>로그인 페이지</h2>
    <span style="color: red; font-size: 1.2em"> 
    <%=request.getAttribute("LoginErr") == null ? "" : request.getAttribute("LoginErr") %>
    </span>
 
    <%
        if(session.getAttribute("UserId") == null) {        //로그인 상태 확인 => 값이 null이면 저장되지 않은 것
                                                            //                     => 로그아웃 상태를 뜻함.
    %>
    <script type="text/javascript">
            function validateForm(form) {
                if(!form.user_id.value) {
                    alert("아이디를 입력해주세요.");
                    return false;
                }
                if(!form.user_pwd.value) {
                    alert("패스워드를 입력해주세요.");
                    return false;
                }
            }
    </script>
 
    <form action="loginProcess.jsp" method="post" name="LoginFrm"
        onsubmit="return validateForm(this);">
        아이디 : <input type="text" name="user_id" class="id"><br> 
        비밀번호 : <input type="password" name="user_pwd"><br>
             <input    type="submit" value="로그인하기" class="btn">
    </form>
    </div>    
    <%                    
        }
        else {    //로그인된 상태
    %>
        <%=session.getAttribute("UserName") %>
        회원님, 로그인하셨습니다.
        <br>
        <a href="logout.jsp">[로그아웃]</a>
    <%
        }
    %>
</body>
</html>
cs

 

 

6. loginProcess.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
<%@page import="kr.co.ezenac.membership.MemberDTO"%>
<%@page import="kr.co.ezenac.membership.MemberDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
    String userId = request.getParameter("user_id");
    String userPwd = request.getParameter("user_pwd");
    
    //web.xml에서 가져온 데이터베이스 연결 정보
    String OracleDriver = application.getInitParameter("OracleDriver");
    String OracleURL = application.getInitParameter("OracleURL");
    String OracleId = application.getInitParameter("OracleId");
    String OraclePwd = application.getInitParameter("OraclePwd");
    
    //회원 테이블에 DAO을 통해 회원 정보 DTO 정보 획득함.
    MemberDAO dao = new MemberDAO(OracleDriver, OracleURL, OracleId, OraclePwd);
    MemberDTO memberDTO = dao.getMember(userId, userPwd);
    dao.close();
    
    if(memberDTO.getId() != null) {
        
        //로그인 성공
        session.setAttribute("UserId", memberDTO.getId());
        session.setAttribute("UserName", memberDTO.getName());
        response.sendRedirect("LoginForm.jsp");
    }
    else {
        //로그인 실패
        request.setAttribute("LoginErr""로그인 오류입니다.");
        request.getRequestDispatcher("LoginForm.jsp").forward(request, response);
        
    }
    
%>
cs

 

 

7. logout.jsp

 

 

1
2
3
4
5
6
7
8
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
    session.invalidate();
    
    //로그인 페이지 이동
    response.sendRedirect("LoginForm.jsp");
%>
cs

 

 

 

< 공부하기 >

 

1. DTO(Data Transfer Object)
   - 데이터를 교환하기 위해 생성하는 객체
   - 멤버변수, getter/setter, 메서드만 갖음.
   - VO(Value Object), 값 객체 


2. DAO(Data Acess Object)
   - 데이터베이스의 데이터에 접근하기 위한 객체
   - JDBC를 통해 구현함.
   - 테이블에서 수행할 수 있는 CRUD를 전담

 

 

Comments