본문 바로가기

콩's EDUCATION/콩's JSP&SERVLET

쿠키_로그인,로그아웃 폼 만들기

<%@ page contentType="text/html; charset=utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>cookie_login.jsp</title>
</head>
<body>
  
<h3 id='header'>사용자 로그인</h3>
 <div id='main' style='text-align:center'>
  <br><br>
  <form method=post>
  <table style='border:1px #0000FF dotted;text-align:center'>
    <tr><td>사용자 ID </td>
      <td><input type=text name=userid></td></tr>
    <tr><td>사용자 암호 </td>
      <td><input type=password name=passwd></td></tr>
    <tr><td>자동로그인 사용 </td>
      <td><input type=checkbox name=cookie></td></tr>   
    <tr><td colspan=2 style='text-align:right'>
   <input type=submit value='로그인'>
   <input type=reset value='취소'></td></tr>
  </table>
 </form>
</div>
 </body></html>

 

결과

 

1. cookie_login.jsp 요청(get방식)

2. userid,passwd 입력 로그인 요청(post 방식)

자동로그인 체크 -> cookie (id, passwd) 저장

3. 브라우저 종료

4. 브라우저 시작 cookie_login.sjp 요청 (get) = 쿠키체크(id,passwd 존재하면 자동 로그인 처리)

 

 

위 로직대로 다시 만들어봤다. cookie_login.jsp

 

<%@ page contentType="text/html; charset=utf-8"%>
<jsp:useBean id="dao" class="com.shop.model.LoginDAO"/>
<%
String uid = null, pwd = null;
String method = request.getMethod();
checklogin:
{
 if(method.equalsIgnoreCase("get")){
  //Get방식으로 접근하는 경우에 쿠키를 체크한다.
 
  Cookie cookies[] = request.getCookies();
  if(cookies!=null){
   for(int i=0;i<cookies.length;i++){
    String name = cookies[i].getName();
    if(name.equals("userid")){
     uid=cookies[i].getValue();
    }else if(name.equals("pwd")){
     pwd = cookies[i].getValue();
    }
   }
   if(uid!=null && pwd!=null){
    if(dao.loginProc(uid,pwd)!=null){
    session.setAttribute("userid", uid);
    session.setAttribute("passwd", pwd);
    break checklogin;   
    }
   }
 }// get방식의 쿠키 객체가 두번째 if end 
%>
<!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>cookie_login.jsp</title>
</head>
<body>
  
<h3 id='header'>사용자 로그인</h3>
 <div id='main' style='text-align:center'>
  <br><br>
  <form method=post>
  <table style='border:1px #0000FF dotted;text-align:center'>
    <tr><td>사용자 ID </td>
      <td><input type=text name=userid></td></tr>
    <tr><td>사용자 암호 </td>
      <td><input type=password name=pwd></td></tr>
    <tr><td>자동로그인 사용 </td>
      <td><input type=checkbox name=cookie></td></tr>   
    <tr><td colspan=2 style='text-align:right'>
   <input type=submit value='로그인'>
   <input type=reset value='취소'></td></tr>
  </table>
 </form>
</div>
 </body></html>
 
<%
return;
} // get방식 처리 end, 다음줄부는 post 방식으로 접근하는 경우에 실행됨

uid= request.getParameter("userid");
pwd = request.getParameter("pwd");
String useCookie = request.getParameter("cookie");

if(dao.loginProc(uid,pwd)==null){ %>
로그인에 실패하셨습니다.
다시<a href=cookie_login.jsp>시도</a>해주세요.
<%
return;
} // post 요청에 대한 로그인 처리
if(useCookie!=null){ // 쿠키설정
 Cookie uidCookie = new Cookie("userid",uid);
 uidCookie.setMaxAge(60*60*24*365);
 response.addCookie(uidCookie);
 Cookie pwdCookie = new Cookie("pwd",pwd);
 pwdCookie.setMaxAge(60*60*24*365);
 response.addCookie(pwdCookie);
}
session.setAttribute("userid",uid);
session.setAttribute("pwd",pwd);
} // 체크로그인, 블록 끝 라벨 괄호.
%>
<html><head><title><%=uid%>님 페이지</title></head><body><h3 id=header><%=uid%>님페이지</h3>
<br><a href="./cookie_logout.jsp">로그아웃</a>
</body></html>

 

cookie_logout.jsp

 

<%@ page contentType="text/html; charset=utf-8"%>
<% session.invalidate(); %>
<%
Cookie[] cookies = request.getCookies();
if(cookies!=null){
 for(int i=0;i<cookies.length;i++){
  if(cookies[i].getName().equals("userid")){
   cookies[i].setMaxAge(0);
   response.addCookie(cookies[i]);
   }
  if(cookies[i].getName().equals("pwd")){
   cookies[i].setMaxAge(0);
   response.addCookie(cookies[i]);
  }  
 }
}
%><script>
alert("로그아웃 되었습니다.");
location.href="./cookie_login.jsp";
</script>