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
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());
cookie.setMaxAge(60*60*24*365);
cookie.setPath("/");
response.addCookie(cookie); etc...
HTH,
James