Quantcast
Channel: Active questions tagged servlets - Stack Overflow
Viewing all articles
Browse latest Browse all 675

In Java servlet, cookie.getMaxAge() always returns -1

$
0
0

If I set a cookie with a setMaxAge() well into the future, when I read the cookie back into memory in a subsequent request, the getMaxAge() gives me back a -1. I have checked the actual cookie via Chrome's settings, and inspector, and I can verify that the expiration date is indeed set 60 days in the future.

static public void setHttpCookie(HttpServletResponse response, String payload) {    Cookie c = new Cookie(COOKIE_NAME, payload);    c.setMaxAge(60*86400); // expire sixty days in the future    c.setPath("/"); // this cookie is good everywhere on the site    response.addCookie(c);}static public String checkForCookie(HttpServletRequest req) {    Cookie[] cookies = req.getCookies();    if ( cookies != null ) {        for ( Cookie c : cookies ) {            if ( COOKIE_NAME.equals(c.getName()) ) {                int maxAge = c.getMaxAge();                logger.debug("Read back cookie and it had maxAge of {}.", maxAge);                String payload = c.getValue();                return payload;            }        }    }    return null;}

Why does c.getMaxAge() always return -1?


Viewing all articles
Browse latest Browse all 675

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>