login.html (회원 인증을 위해 아이디와 비밀번호를 입력받는 폼)
|
<!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>회원 인증을 위해 아이디와 비밀번호를 입력받는 폼</title> </head> <body> <center> <form method = "post" action = "testLogin.jsp"> <table> <tr> <td align = "center">아이디</td> <td align = "left"> <input type = "text" name = "id" size = "20" maxlength = "20"> </td> </tr> <tr> <td align = "center">패스워드</td> <td align = "left"> <input type = "password" name = "pw" size = "20" maxlength = "20"> </td> </tr> <tr> <td align = "center"><input type = "submit" value = "로그인"></td> <td align = "center"><input type = "reset" value = "취 소"></td> </tr> </table> </form> </center> </body> </html>
|
출력화면 |
![](https://t1.daumcdn.net/cfile/tistory/14183D3C4FC9EFF23A)
|
testLogin.jsp (회원 인증 처리를 위한 JSP 페이지)
|
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String s_id = "yinglong200"; String s_pw = "q1w2e3r4"; String s_name = "Hayate"; if (s_id.equals(request.getParameter("id")) && s_pw.equals(request.getParameter("pw"))) { Cookie cookie = new Cookie("memName", s_name); cookie.setMaxAge(20*60); response.addCookie(cookie); response.sendRedirect("main.jsp"); } else { response.sendRedirect("login.html"); } %> <!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>회원 인증 처리를 위한 JSP 페이지</title> </head> <body> </body> </html>
|
main.jsp (로그인 인증 받은 회원에게 제공되는 페이지)
|
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import = "myUtil.HanConv" %> <% String s_name = ""; try { // 쿠키를 얻는다. Cookie[] cookies = request.getCookies(); if (cookies != null) { for (int i = 0; i < cookies.length; i++) { if (cookies[i].getName().equals("memName")) { s_name = cookies[i].getValue(); } } if (s_name.equals("")) { response.sendRedirect("login.html"); } } else { response.sendRedirect("login.html"); } } catch (Exception e) { e.printStackTrace(); } %> <!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>로그인 인증 받은 회원에게 제공되는 페이지</title> </head> <body> <center> <form method = "post" action = "logOut.jsp"> <table> <tr> <td align = "center"> <%= HanConv.toKor(s_name) %>님 안녕하세요! </td> </tr> <tr> <td> 저희 홈페이지에 방문해 주셔서 감사합니다.<br/> 즐거운 시간 되세요...<br/> </td> </tr> <tr> <td align = "center"> <input type = submit value = "로그아웃"> </td> </tr> </table> </form> </center> </body> </html>
|
출력화면 |
![](https://t1.daumcdn.net/cfile/tistory/204AD33C4FC9F09C01)
|
logOut.jsp (인증된 사용자의 인증을 무효화 하는 JSP)
|
<%@ 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>인증된 사용자의 인증을 무효화하는 JSP 페이지</title> </head> <body> <% //쿠키에서 값을 얻는다. Cookie[] cookies = request.getCookies(); if (cookies != null) { for (int i = 0; i < cookies.length; i++) { if (cookies[i].getName().equals("memName")) { cookies[i].setMaxAge(0); response.addCookie(cookies[i]); } } } %> <script> alert("로그 아웃 되었습니다."); location.href = "testLogin.jsp"; </script> </body> </html>
|
출력화면 |
![](https://t1.daumcdn.net/cfile/tistory/182D264C4FC9F1422E)
|
풀이 |
![](https://t1.daumcdn.net/cfile/tistory/113C0A344FC9F34A06)
| login.html 페이지에서 아이디와 패스워드를 입력받아 testLogin.jsp에서 회원인지를 판단하여 아이디가
"yinglong200"이고 패스워드가 "q1w2e3r4"로 일치하면 쿠키에 사용자 이름을 저장한 후 main.jsp로 넘어간다. |