2019.02.01 Cookie와 Session의 활용 : 로그인 유지 상태
2019. 2. 1. 13:25ㆍJSP
#1 sessionLogin.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 | <%@ 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> <% // 로그인 유지작업 Cookie[] cookies = request.getCookies(); if(cookies != null ){ for(Cookie cookie : cookies){ if(cookie.getName().equals("id")){ /* 실행흐름이 서버에 있을 경우 서버코드로써 강제이동 서버에서 클라이언트()에게 특정페이지로 이동하는 정보만 응답으로 준다. java코드로 페이지 이동 -> sendRedirect("url"); */ response.sendRedirect("sessionMain.jsp"); } } } %> <h1>로 그 인</h1> <form action="sessionLoginPro.jsp" method="post"> 아 이 디: <input type="text" name="id"><br> 패스 워드: <input type="password" name="pw"><br> <input type="checkbox" name ="loginChk" value="true">로그인 상태유지<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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | <%@ 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 loginChk =request.getParameter("loginChk"); System.out.println(loginChk); // 기존 데이터베이스의 사용자아이디, 패스워드 String dbId = "박보검"; String dbPasswd = "1234"; // 기존정보와 사용자입력정보를 비교 // 정보가 일치할 경우 로그인 == 세션값 생성 "id" id / 이동 ssesionMain.jsp if(id.equals(dbId)){ }else { out.println("아이디가 일치하지않음.\n"); %> <script> alert('아이디가 일치하지않음'); location.href='sessionLogin.jsp'; </script> <% } if(pw.equals(dbPasswd)){ // 로그인작업 - 세션생성 session.setAttribute("id", id); // 사용자의 로그인 유지여부를 체크박스null확인으로 한다.-> checkbox는 체크되지않으면 null값이 리턴된다. if(loginChk != null){ // null값이 아니라면 id값을 쿠키로 저장 Cookie cookie = new Cookie("id",id); cookie.setMaxAge(60*2); cookie.setPath("/"); response.addCookie(cookie); } %> <script> alert('로그인이 인증됨\n 메인페이지로 이동'); location.href='sessionMain.jsp'; </script> <% } else{ out.println("비밀번호가 일치하지않음."); %> <script> alert('비번호가 일치하지않음'); location.href='sessionLogin.jsp'; </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 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | <%@ 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"); %> <% // 이미 로그인된 사용자가 Main.jsp로 넘어오면 쿠키로 사용자를 찾아 세션으로 주입 // 쿠키값을 세션으로 주입해 , 대신하여 로그인과정을 진행한다. Cookie[] cookies = request.getCookies(); if(cookies != null){ for(Cookie cookie : cookies){ if(cookie.getName().equals("id")){ session.setAttribute("id", cookie.getValue()); } } } %> <h1>메인페이지</h1> <%=id %>님이 로그인 하였습니다 <br> <input type="button" value="로그아웃" onclick="location.href='sessionOut.jsp'"> </body> </html> | cs |
#4 sessionOut.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"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>로그아웃</title> </head> <body> <% // 세션 초기화 session.invalidate(); // 로그인 유지 쿠키 삭제 Cookie[] cookies = request.getCookies(); if(cookies != null){ for(Cookie cookie : cookies){ if(cookie.getName().equals("id")){ cookie.setMaxAge(0); cookie.setPath("/"); response.addCookie(cookie); } } } %> <script> alert('로그아웃됨.세션이 초기화됨'); location.href='sessionLogin.jsp'; </script> </body> </html> | cs |
#5 실행화면
->> checkbox에 체크되면 id를 기억하는 쿠키가 생성됨
'JSP' 카테고리의 다른 글
2019.02.01 JDBC: SQL문 사용하기 (SELECT) (0) | 2019.02.01 |
---|---|
2019.02.01 JDBC의 연동 (0) | 2019.02.01 |
2019.02.01 Cookie 만들기(2) (0) | 2019.02.01 |
2019.01.31 Session의 활용 : 로그인페이지 (0) | 2019.01.31 |
2019.01.31 Session의 이해 (0) | 2019.01.31 |