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
How to update a cookie. String name = "Cookie name" ; String value = "New value" ; Cookie cookie = new Cookie(name, value);
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.
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 .
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?
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With