COOKIE

easyliving·2022년 12월 29일
0

FRONT_END (JAVA_SCRIPT)

목록 보기
17/55

쿠키 저장

<%@ 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>
		<h2>쿠키 저장</h2>
		<p>cookieId,cookiePw 2개의 파일을 쿠키를 저장합니다.</p>
		<%
			//1개 쿠키 생성
			Cookie cookie=new Cookie("cookieId","aaa"); //name,value
			Cookie cookie2=new Cookie("cNickName","smile");
			cookie.setMaxAge(60*60); //1시간
			cookie2.setMaxAge(60*60);
			response.addCookie(cookie);
			response.addCookie(cookie2);
		%>
		 
		<button><a href="jsp1226_11_read.jsp">저장한 쿠키 읽기</a></button>
		
	</body>
</html>

쿠키 읽기

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>cookie read</title>
	</head>
	<body>
		<%
			Cookie[] cookies=request.getCookies();
			for(int i=0; i<cookies.length;i++){
				out.println("쿠키 변수명:"+cookies[i].getName()+"<br>");
				out.println("쿠키 변수값:"+cookies[i].getValue()+"<br>");
				out.println("쿠키 유효시간:"+cookies[i].getMaxAge()+"<br>");
				out.println("<br>====================================================================<br>");
			}
		%>
		
		<button><a href="jsp1226_11_delete.jsp">저장한 쿠키 삭제</a></button>
	</body>
</html>

쿠키 삭제

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>cookie delete</title>
	</head>
	<body>
	<%
		Cookie[] cookies=request.getCookies();
		//	쿠키 모두 삭제
		for(int i=0;i<cookies.length;i++){
			cookies[i].setMaxAge(0); //유효시간을 0으로 줘서 삭제
			response.addCookie(cookies[i]);
		}
	
	//특정 쿠키만 삭제
		for(int i=0;i<cookies.length;i++){
			if(cookies[i].getName().equals("cookieId")){
				cookies[i].setMaxAge(0);
				response.addCookie(cookies[i]);
			}
		}
				

	%>
	<button><a href="jsp1226_11_read.jsp">저장한 쿠키 읽기</a></button>
	</body>
</html>

공부

profile
가끔져요

0개의 댓글