블로그 이미지
암초보

calendar

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

Notice

Tag

05-05 20:18
2009. 8. 4. 09:02 프로그래밍/Servlet&JSP

저장

 

 Cookie cook1 = new Cookie("name", "kalzas");  //쿠키객체생성

 cook1.setPath="/";  //쿠키가 적용될 웹서버의 url 경로

 cook1.setMaxAge(60*60*24*365); //쿠키가 유지되는 시간(1년) -1일경우 페이지 닫으면 삭제

 cook1.setDomain = "url"; //응답해 줄 도메인 명

 response.addCookie(cook1);  //쿠키를 클라이언트에 세팅

  

 -가져오기

 

 try(

   Cookie[] cookies = request.getCookies();

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

     out.println(cookies[i].getName() + "은" + cookies[i].getValue() + "입니다.);

   }

 }catch (Exception e){

   out.println(e);

 }

 

 -삭제하기

 

 try{

   Cookie[] cookies = request.getCookies();

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

     cookies[i].setMaxAge(0);

     cookies[i].setPath("/");              // 처음 설정한 Path 로 해줘야 쿠키가 삭제된다!

     response.addCookie(cookies[i]);

   }

   out.println("쿠키가 삭제되었습니다.");

 }catch(Exception e){

   out.println(e);

 }

 

특정 도메인과 연결된 경우에는 그 도메인의 요청에 의해서만 삭제해야 한다.

 

try{

   Cookie[] cookies = request.getCookies();

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

        Cookie cook = cookies[i];

        if(cook.getName().equals("ucc_login")) {

           cook.setPath = "/";

           cook.setDomain = "url"; 

           cook.setMaxAge(0);

           response.addCookie(cook);
        }

     }

} catch(Exception e) {

   out.println(e);

 }

'프로그래밍 > Servlet&JSP' 카테고리의 다른 글

<c:forEach>에서 인덱스 사용법  (0) 2013.11.27
fmt:parseDate 와 fmt:formatDate  (0) 2013.05.20
URLDecoder  (0) 2009.08.05
posted by 암초보