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>

쿠키로그인폼에서 날린 아이디를 쿠키로그인 프로시저 에서 받아서 쿠키로 저장하고 응답에 쿠키 아이디 키와 value 를 던집니다. 돌아오면 쿠키 값을 배열로 저장해서 쿠키객체 생성하고 값이 들어가 있으면 for문으로 돌립니다. 쿠키의 값이 "id" 의 value와 같다면 id에 에 "id"의 값을 넣습니다

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 이 아니라서 쿠키 객체생성하고 시간, 클라이언트에게
아이디를 넘겨준다

profile
건물주가 되는 그날까지

0개의 댓글