JSP Servlet 기초(9) 세션을 이용한 로그인

이번엔 세션을 이용한 간단한 로그인 기능을 구현해보겠습니다.

먼저 이클립스에 

login.html, home.jsp, logout.jsp, sessionTest.jsp, loginGo.jsp 의

5개 파일을 생성합니다


먼저 login,html 입니다


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
 
    <form action="loginGo.jsp" method="post">
        아이디 : <input type="text" name="id" size="10"><br />
        비밀번호 : <input type="password" name="pw" size="10"><br />
        <input type="submit" value="로그인">
    </form>
    
</body>
</html>
cs


간단하게 아이디와 비밀번호 입력할 input 태그 생성후 loginGo.jsp 로 action 합니다.


다음 loginGo.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
<%@ 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>
    <%!
        String id, pw;
    %>
    <%
        id = request.getParameter("id");
        pw = request.getParameter("pw");
        
        if(id.equals("abcde"&& pw.equals("12345")) {
            session.setAttribute("id", id);    
            response.sendRedirect("home.jsp");
        } else {
            response.sendRedirect("login.html");
        }
    %>
 
</body>
</html>
 
cs


login.html 에서 입력한 id 와 pw 를 request.getParameter 로 요청합니다

다음 if 문에 조건을주어 id가 abcde, pw가 12345 로입력 되었다면

세션을 만들어 세션에 id를 저장합니다.(즉 세션엔 id 값인 abcde가 저장된겁니다)

다음 home.jsp로 sendRedirect 시킵니다.

만약 위의 id와 pw로 로그인하지 않았아면 else 에 의해 login.html로

sendRedirect 됩니다.


home.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
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.util.Enumeration"%>
<!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>
 
    <%
        Enumeration enumeration = session.getAttributeNames();
        while(enumeration.hasMoreElements()){
            String sName = enumeration.nextElement().toString();
            String sValue = (String)session.getAttribute(sName);
            
            if(sValue.equals("abcde")) out.println(sValue + "님 안녕하세요." + "<br />");
        }
    %>
    
    <a href="logout.jsp">로그아웃</a>
    
</body>
</html>
 
cs


열거형 인터페이스로 세션의 이름을 가져옵니다.

15,16line은 enumeration 에 저장된 요소를 toString 을사용하여

문자열로 변환후 sName 에 세션이름을 저장합니다.

다음 16line 에서 세션의 이름으로부터 getAttribute 하여

세션의 value 값을 String형으로 변환하여 sValue 로 저장합니다.


후에 if 조건을 걸어 sValue 와(sValue 에는 세션의value 값이 저장되어있다)

abcde 를 equals로 비교하여 true가 반환된다면 out.println 하여 화면에 출력시킵니다.


logout.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
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.util.Enumeration"%>   
<!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>
    
    <%
        Enumeration enumeration = session.getAttributeNames();
        while(enumeration.hasMoreElements()) {
            String sName = enumeration.nextElement().toString();
            String sValue = (String)session.getAttribute(sName);
            
            if(sValue.equals("abcde")) session.removeAttribute(sName);
        }
        
    %>
    
    <a href="sessionTest.jsp">sessionTest</a>
    
</body>
</html>
 
cs


18line 처럼 abcde 값의 세션을 삭제하고 sessionTest.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
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.util.Enumeration"%>    
<!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>
 
    <%
        Enumeration enumeration = session.getAttributeNames();
        int i = 0;
        while(enumeration.hasMoreElements()) {
            i++;
            
            String sName = enumeration.nextElement().toString();
            String sValue = (String)session.getAttribute(sName);
            
            out.println("sName : " + sName + "<br />");
            out.println("sValue : " + sValue + "<br />");
        }
        
        if(i == 0out.println("해당 세션이 삭제 되었습니다.");
        
    %>
 
</body>
</html>
 
 
cs


sessionTest.jsp 에서는 


남아있는 세션을 출력한다.

14line에 int i=0; 을 주고 while 문 안에 후위연산자를 주었다.

만약 세션이 있다면 while 문 을 타게되어 i 값이 1 증가되어  

25line 의 조건에 벗어나 "해당 세션이 삭제 되었습니다." 라는 출력문이 나오지 않을것이고

세션이 삭제 되었다면  while 문을 안타게 되면서 i의 값이 0으로 유지되어

25line의 출력문이 화면에 나올것이다.



'JSP' 카테고리의 다른 글

JSP Servlet 기초(11) JDBC  (0) 2018.01.31
JSP Servlet 기초(10) 자바 빈  (0) 2018.01.30
JSP Servlet 기초(8) 세션  (1) 2018.01.29
JSP Servlet 기초(7) 쿠키를 이용한 로그인  (0) 2018.01.29
JSP Servlet 기초(6) 쿠키생성  (2) 2018.01.29

댓글

Designed by JB FACTORY