블로그 이미지
헤이장

Notice

Recent Post

Recent Comment

Recent Trackback

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
  • total
  • today
  • yesterday
2009. 7. 27. 13:39 Web/Jsp

로그인 시 아이디 저장하기 옵션 처럼
아이디를 저장해두고 싶은 경우
쿠키를 사용 하면 된다 

쿠키 저장하기 

Cookie cookie = new Cookie("id",id);
객체 생성

cookie.setMaxAge(-1);
브라우저가 종료될때까지 유지 

cookie.setMaxAge(24*60*60);
단위는 초
24시간동안 쿠키를 유지하게 한다
이렇게 하면 일주일 한달이라도 id 값을 쿠키로 가지고 있을 수 있다 

response.addCookie(cookie);
쿠키를 응답에 저장
 

쿠키에서 불러오기 

Cookie[] cookie = req.getCookies();
int cLen = cookie.length;
for (int i = 0; i < cLen; i++) {
    if (cookie[i].getName().equals("id")) {
        System.out.println("세션에 저장된 id 값은? " + cookie[i].getValue());
    }
}

posted by 헤이장