JSP/COOKIE
chapter05 : id_check1 / id_check2
GAWON
2023. 5. 24. 09:19
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//"save_id" 쿠키 확인
Cookie[] cookiebox = request.getCookies();
String id= null;
if(cookiebox != null && cookiebox.length > 0){
for(int i=0; i<cookiebox.length; i++){
if(cookiebox[i].getName().equals("save_id")){
id=cookiebox[i].getValue(); //아이디에 변수값 부여
break;
}
}
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="Ex05_id_check2.jsp">
<%if(id==null){%>
아이디 : <input type="text" name="id">
<%}else{%>
아이디 : <input type="text" name="id" value="<%=id%>">
<%}%>
<br><br>
<input type="checkbox" name="save_id">아이디 기억하기
<br>
<input type="submit" value="전송">
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
Cookie cookie = null;
String id = request.getParameter("id");
String save_id = request.getParameter("save_id");
//아이디 기억하기를 체크 및 체크 해제 했을 때
if(save_id!=null){
cookie = new Cookie("save_id", id);
cookie.setMaxAge(60*60*24);
response.addCookie(cookie);
}else{
//기존에 만들었던 "save_id" 쿠키 확인 후 삭제
Cookie[] cookiebox = request.getCookies();
if(cookiebox != null && cookiebox.length > 0){
for(int i=0; i<cookiebox.length; i++){
if(cookiebox[i].getName().equals("save_id")){
Cookie bisket = new Cookie("save_id","");
bisket.setMaxAge(0);
response.addCookie(bisket);
break;
}
}
}
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="Ex05_id_check1.jsp">
<input type ="submit" value="로그인 화면 돌아가기">
</form>
</body>
</html>