반응형
Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
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
관리 메뉴

비전공개미 개발노트

[JSP] 쿠키를 설정하고 확인하기 본문

프로그래밍/JSP

[JSP] 쿠키를 설정하고 확인하기

비전공개미 2022. 10. 12. 20:06
반응형
SMALL

setCookie.jsp

<%@ 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>
<%
Cookie c = new Cookie("appleCookie", "apple"); //cookiet설정
c.setMaxAge(60*1); //쿠키시간설정
c.setValue("tea"); //쿠키값을 설정
response.addCookie(c); //setValue()를 지정해준후 꼭 작성해줘야 함
%>
<h3>---쿠키 생성---</h3>
<p>쿠키 내용 확인은 <a href="tasteCookie.jsp">여기로</a>!!!</p>
</body>
</html>

 

 

tasteCookie.jsp

<%@ 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>
<h3>---쿠키 정보 확인---</h3>
<%
Cookie[] c = request.getCookies(); //쿠키를 배열로 불러온다
if(c != null){
	out.print("현재 설정된 쿠키의 수 : "+ c.length);
	out.print("<br>=======================================<br>");
	for(int i=0; i<c.length; i++){
		//out.print("쿠키 속성 이름["+i+"] : "+c[i].getName()+"<br>");
		//out.print("쿠키 속성 값["+i+"] : "+c[i].getValue()+"<br>");
		c[i].setMaxAge(0); //쿠키값 초기화
		response.addCookie(c[i]); //setMaxAge를 사용후 response.addCookie()필수로 입력
		out.print("------------------------------------------------------------------<br>");
	}
	//response.sendRedirect("tasteCookie.jsp");
}
%>
</body>
</html>

 

 

[출력]

반응형
LIST
Comments