1. 쿠키 생성

cookieCreate.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"

    pageEncoding="EUC-KR"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">

<title>Insert title here</title>

</head>

<body>

<% Cookie cookie = new Cookie("cookieName","cookieVel");

cookie.setMaxAge(60*60); // 유효기간 설정 (초)

cookie.setSecure(true); // 쿠키를 SSL 이용할때만 전송하도록

cookie.setHttpOnly(true); // https 일때(보안)  

response.addCookie(cookie);

%>


<a href="cookieGet.jsp">cookie Get</a>

</body>

</html>

----------------------------------------------------------------------------------------------------------------------------

2. 쿠키 가져오기

cookieGet.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">

<title>Insert title here</title>

</head>

<body>

<%

Cookie[] cookies = request.getCookies();    //생성된 쿠키를 가져온다.

for(int i= 0; i<cookies.length;i++){

out.println("cook["+i+"] name="+cookies[i].getName()+"<br/>");

out.println("cook["+i+"] val="+cookies[i].getValue()+"<br/>");

}

%>

<a href="cookieDel.jsp">cookie Del</a>

</body>

</html>

----------------------------------------------------------------------------------------------------------------------------

3. 쿠키 삭제

cookieDel.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"pageEncoding="EUC-KR"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">

<title>Insert title here</title>

</head>

<body>

<%

Cookie[] cookies = request.getCookies();

for (int i = 0; i < cookies.length; i++) {

if (cookies[i].getName().equals("cookieName")) {

cookies[i].setMaxAge(0);   // 유효시간을 0으로 설정함으로써 쿠키를 삭제 시킨다.  

response.addCookie(cookies[i]);

}

}

%>

<a href="cookieGet.jsp">cookie Get</a>

</body>

</html>




728x90
반응형

+ Recent posts