2019.01.31 Session의 활용 : 로그인페이지

2019. 1. 31. 16:51JSP

#1 sessionLogin.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>sessionLogin</title>
</head>
<body>
    <form action="sessionLoginPro.jsp" method="post">
        아  이  디: <input type="text" name="id"><br>
        패스 워드: <input type="password" name="pw"><br>
        <input type ="submit" value ="login">
    </form>
 
</body>
</html>
cs



#2 sessionLoginPro.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
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Information</title>
</head>
<body>
    <%
        //post방식 - 전송데이터를 한글처리해야된다.
        request.setCharacterEncoding("utf-8");
        
        // 파라미터값 가져오기 
        String id = request.getParameter("id");
        String pw = request.getParameter("pw");
        
        // 기존 데이터베이스의 사용자아이디, 패스워드
        String dbId = "박보검";
        String dbPasswd = "1234";
        // 기존정보와 사용자입력정보를 비교
        // 정보가 일치할 경우 로그인 == 세션값 생성 "id" id / 이동 ssesionMain.jsp
        if(id.equals(dbId)){
            
        }else {
            out.println("아이디가 일치하지않음.\n");
            %>
            <script>alert('아이디가 일치하지않음');</script>
            <%
        }
        
        if(pw.equals(dbPasswd)){
            // 로그인작업 - 세션생성
             session.setAttribute("id", id);
                %>
                <script>
                alert('로그인이 인증됨\n 메인페이지로 이동');
                location.href="sessionMain.jsp";    
                </script>
                <%
        
        }else{
            out.println("비밀번호가 일치하지않음.");
            %>
            <script>alert('비번호가 일치하지않음');</script>
            <%
        }
        
        %>
</body>
</html>
cs


#3 sessionMain.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Main</title>
</head>
<body>
    <% 
        //세션값 가져오기
        String id = (String)session.getAttribute("id");
    %>
    <h1>메인페이지</h1>
    <%=id %>님이 로그인 하였습니다<br>
    
    <input type="button" value="로그아웃"  onclick="location.href='sessionOut.jsp'">
 
</body>
</html>
cs




#4 실행결과








>>비빌번호가 일치하지 않을 경우 alert가 발생, 마찬가지로 아이디도 alert가 발생



'JSP' 카테고리의 다른 글

2019.02.01 Cookie와 Session의 활용 : 로그인 유지 상태  (0) 2019.02.01
2019.02.01 Cookie 만들기(2)  (0) 2019.02.01
2019.01.31 Session의 이해  (0) 2019.01.31
2019.01.31 get형식 & post형식  (0) 2019.01.31
2019.01.30 Cookie 만들기(1)  (0) 2019.01.30