We Recommend

Java How to Program Java How to Program
Takes a new tools-based approach to Web application development that uses Netbeans 5.5 and Java Studio Creator 2 to create and consume Web Services. Features new AJAX-enabled, Web applications built with JavaServer Faces (JSF), Java Studio Creator 2 and the Java Blueprints AJAX Components. Includes new topics throughout, such as JDBC 4, SwingWorker for multithreaded GUIs, GroupLayout, Java Desktop Integration Components (JDIC), and much more.


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.