2013년 7월 12일 금요일

(130712) 15일차 SetCookie.jsp, GetCookie.jsp (JSP Cookie 테스트)

 - Cookie 
 : 통행증
 : 클라이언트가 웹서버를 보기위해서 요청하면 웹서버는 쿠키라는 것을 줌으로 써, 다음번 실행 때는 쿠키만으로 바로 웹서버를 볼 수 있도록 해준다.

 - Cookie의 구성요소

 : 이름 (각각의 쿠키를 구별하는 데 사용되는 이름)
 : 값 (쿠키의 이름과 관련된 값)
 : 유효 시간 (쿠키의 유지 시간)
 : 도메인 (쿠키를 전송할 도메인)
 : 경로 (쿠키를 전송할 요청 경로)


 - SetCookie.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("coociename", "samsung"); // 쿠키 이름, 쿠키 값
response.addCookie(cookie);
%>

쿠키 이름은 <%=cookie.getName()%> 이고 값은 <%=cookie.getValue()%>
</body>
</html>


 - 결과








 - GetCookie.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 = request.getCookies();

if(cookie != null && cookie.length > 0){
for(int i = 0 ; i < cookie.length ; i++){
%>
이름은 <%=cookie[i].getName()%> 이고 값은 <%=cookie[i].getValue()%> 이다. <br>
<%
cookie[i].setDomain("localhost.com");
%>
도메인은 <%=cookie[i].getDomain()%> <br>
패스는 <%=cookie[i].getPath()%> <br>
<% }
}
else{
%>
쿠키가 없습니다. <br>
<%
}
%>
</body>
</html>


 - 결과