Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

WON.dev

chapter05 : id_check1 / id_check2 본문

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>

'JSP > COOKIE' 카테고리의 다른 글

chapter04 : cookie_remove  (0) 2023.05.24
chapter03 : cookie_change  (0) 2023.05.24
chapter02 : cookie_view  (0) 2023.05.24
chapter01 : cookie_make  (0) 2023.05.24
cookie  (0) 2023.05.24