본문 바로가기
🎵JSP

[JSP] Cookie(쿠키생성, 시간설정, 보내기,다른페이지)

by 김말자 2023. 2. 9.
728x90
728x90
BIG

쿠키 객체 생성

<%@ 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 cookie = new Cookie("id","malja");
//쿠키 시간 설정 1초
cookie.setMaxAge(60*60);//1시간
//레스폰스에 쿠키를 담아서 보냄
response.addCookie(cookie);
//다른 페이지에서도 가능
response.sendRedirect("cookConfirm.jsp");
%>
</body>
</html>
<%@ 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[] cookies = request.getCookies();
for(Cookie c : cookies){
	out.print(c.getName()+" - "+c.getValue()+"<br>");
}


%>
</body>
</html>

 

 

728x90
반응형
BIG

댓글