JSP/2012.04강의(MySQL)
3일차 Cookie
Bohemian life
2012. 5. 23. 12:39
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.net.*" %> <!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=UTF-8"> <title>쿠키</title> </head> <body> <pre> 사용용도 : 접속시간 : 1시 접속해제 시간: 2시 프로그램 설치 날짜 </pre> 쿠키를 생성하자 <% Cookie cookie = new Cookie("name",URLEncoder.encode("Lee")); cookie.setMaxAge(600000);//유효 기간 response.addCookie(cookie); %> <br/> 만들어진 쿠키 확인하기<br/> 쿠키 name : <%=cookie.getName() %><br/> 쿠키 값 : <%=cookie.getValue() %> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.net.*" %> <!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=UTF-8"> <title>사용자의 쿠키를 체크하자</title> </head> <body> <% //요청 request 영역에는 쿠키가 자동으로 포함됩니다. Cookie[] cookies = request.getCookies(); String id=""; String name=""; for(int i=0;i<cookies.length;i++){ name = cookies[i].getName(); id = cookies[i].getValue(); /* URL 인코딩 했으므로 URL 디코딩을 하자 */ id=URLDecoder.decode(id,"UTF-8"); if(id!=null){ out.pritt("쿠키가 존재합니다"); out.print("이 페이지에 접속한 적이 있습니다."); } } %> <br/>쿠키의 개수는 : <%=cookies.length %><br/> 쿠키의 네임값은 : <%=name %><br/> 쿠키의 value는 : <%=id %><br/> <%String test="안녕하세요"; test = URLEncoder.encode(test,"UTF-8"); %> 인코딩결과 : <%=test %> <%test =URLDecoder.decode(test,"UTF-8");%> 디코딩결과 : <%=test %> </body> </html>