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

后端 未结 2 1301
慢半拍i
慢半拍i 2020-12-09 14:51

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

2条回答
  •  一整个雨季
    2020-12-09 15:31

    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.

提交回复
热议问题