Posted By

jsbournival on 08/30/07


Tagged

get java cookie set cookies servlet


Versions (?)

get/set Cookies


 / Published in: Java
 

URL: http://www.commentcamarche.net/servlets/servcookie.php3

Simple snippet on how to get/set a cookie

  1. // retrieve cookies
  2. Cookie[] cookies = request.getCookies();
  3.  
  4. for(int i=0; i < cookies.length; i++) {
  5. Cookie c = cookies[i];
  6.  
  7. if (c.getName().equals("theCookieImLookingFor")) {
  8. String cookieValue = c.getValue();
  9. }
  10. }
  11.  
  12. // ...
  13.  
  14. // set a new cookie
  15. Cookie myCookie = new Cookie("name", "value");
  16. response.addCookie(myCookie);

Report this snippet  

You need to login to post a comment.