JSP/COOKIE

chapter03 : cookie_change

GAWON 2023. 5. 24. 09:17
<%@page import="java.net.URLEncoder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<!-- 
		쿠키 값 변경을 덮어쓰기이다. 먼저 쿠키가 존재하는지 확인 필요
		
		1.이름이 name인 쿠키의 값을 "마이클 조던"으로 변경
		2.유효 시간을 15일로 변경
		3.쿠키 저장소에 저장
	 -->
	<%
		
		Cookie[] cookiebox = request.getCookies();
		
		if(cookiebox != null && cookiebox.length > 0){
			for(int i=0; i<cookiebox.length; i++){
				if(cookiebox[i].getName().equals("name")){		
				Cookie bisket = new Cookie("name", URLEncoder.encode("마이클 조던", "utf-8"));
				bisket.setMaxAge(60*60*24*15);
				response.addCookie(bisket);
				out.print("쿠키 값을 변경하였습니다");
			}
		}
	}else{
		out.print("쿠키가 존재하지 않습니다.");
		}
	
	
	%>

</body>
</html>