Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a Java Servlet how can I change the value of an existing cookie?

In a Java Servlet how can I change the value of an existing cookie? There is an addCookie method, but no deleteCookie or editCookie in HttpServletResponse

like image 749
yazz.com Avatar asked Sep 16 '11 16:09

yazz.com


People also ask

How do I change the value of a cookie in Java?

How to update a cookie. String name = "Cookie name" ; String value = "New value" ; Cookie cookie = new Cookie(name, value);

How are cookies handled in Java servlets?

Cookies are text files stored on the client computer and they are kept for various information tracking purpose. Java Servlets transparently supports HTTP cookies. Server script sends a set of cookies to the browser. For example name, age, or identification number etc.

How do you remove a cookie in a Java servlet?

The Servlet API doesn't provide a direct way to delete a cookie in a Servlet application. If you want to delete a cookie you have to create a cookie that have the same name with the cookie that you want to delete and set the value to an empty string. You also need to set the max age of the cookie to 0 .

Which code is used for cookies in servlet?

getCookies() returns an array containing all of the Cookie objects the client sent with this request. Q 23 - Which of the following code is used to get names of the attributes in servlet?


2 Answers

Those indeed don't exist. Just create utility methods yourself which do that. Particularly getting the desired cookie is quite bloated. E.g.

public final class Servlets {      private Servlets() {}      public static Cookie getCookie(HttpServletRequest request, String name) {         if (request.getCookies() != null) {             for (Cookie cookie : request.getCookies()) {                 if (cookie.getName().equals(name)) {                     return cookie;                 }             }         }          return null;     }  } 

To edit a cookie, set its value and then add it to the response:

Cookie cookie = Servlets.getCookie(request, "foo");  if (cookie != null) {     cookie.setValue(newValue);     response.addCookie(cookie); } 

Set if necessary the maxage, path and domain as well if they differs from your default. The client namely doesn't send this information back.

To delete a cookie, set the max age to 0 (and preferably also the value to null):

Cookie cookie = Servlets.getCookie(request, "foo");  if (cookie != null) {     cookie.setMaxAge(0);     cookie.setValue(null);     response.addCookie(cookie); } 

Set if necessary the path and domain as well if they differs from your default. The client namely doesn't send this information back.

like image 73
BalusC Avatar answered Sep 23 '22 13:09

BalusC


Here is an example from kodejava:

 public class ReadCookieExample extends HttpServlet {  protected void doPost(HttpServletRequest request, HttpServletResponse   response)         throws    ServletException, IOException {     response.setContentType("text/html");     PrintWriter writer = response.getWriter();      Cookie[] cookies = request.getCookies();     for (int i = 0; i < cookies.length; i++) {         writer.println("Name: " + cookies[i].getName() + "; Value: "                    +                   cookies[i].getValue());                 } }  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws    ServletException, IOException {     doPost(request, response); } 

}

That will get the cookies list, get the one you want and instead of printing out values, do something similar to this:

 cookie.setValue(String.valueOf(<new Value>));    cookie.setMaxAge(60*60*24*365);     cookie.setPath("/");     response.addCookie(cookie);  etc... 

HTH,

James

like image 44
James Drinkard Avatar answered Sep 22 '22 13:09

James Drinkard