CookieLoginForm.jsp
<body>
<%
//사용자 컴퓨터의 쿠키 저장소로부터 쿠키값을 읽어들입니다 몇개인지 몰라 배열을 이용해서 쿠키값을 저장
Cookie[] cookies = request.getCookies();
String id = "";
// 쿠키 값이 없을 수도 있기에 null 처리 해줍니다.
if(cookies != null){
for(int i=0 ; i <cookies.length ; i++){
if(cookies[i].getValue().equals("id")){
id = cookies[i].getValue();
break;//원하는 데이터를 찾았기에 반복문을 빠져나옴
}
}}
%>
<center>
<h2>쿠키 로그인</h2>
<form action="CookieLoginProc.jsp" method = "post">
<table width="400" border="1">
<tr height="50">
<td width ="150">아이디</td>
<td width ="250"><input type="text" name="<%=id %>"></td>
</tr>
<tr height="50">
<td width ="150">패스워드</td>
<td width ="250"><input type="password" name="pass"></td>
</tr>
<tr height="50">
<td colspan="2" align="center">
<input type="checkbox" name="save">아이디 저장</td>
</tr>
<tr height="50">
<td colspan="2" align="center">
<input type="submit" value="로그인"></td>
</tr>
</table>
</form>
</center>
CookieLoginProc.jsp
<body>
<%
//아이디 저장 체크 박스가 체크되었는지 판단여부
String save = request.getParameter("save");
String id =request.getParameter("id");
if(save !=null) {
Cookie cookie = new Cookie("id",id); //1번째 String 키값을 적어줌, 2번째는 value 값을 넣어줌
//쿠키 유효시간 설정
cookie.setMaxAge(60*10);//10분간
//사용자에게 쿠키 값을 넘겨주다
response.addCookie(cookie);
}
out.print("쿠키생성 완료");
%>
</body>
넘어온 아이디저장 값이 null 이 아니라서 쿠키 객체생성하고 시간, 클라이언트에게
아이디를 넘겨준다