Difference between 0 and negative value for setMaxAge for Cookie

后端 未结 3 1954
感动是毒
感动是毒 2020-12-16 00:57

From cookie docs for setMaxAge(int): http://docs.oracle.com/javaee/1.3/api/javax/servlet/http/Cookie.html#setMaxAge(int) Is there any difference between int = 0

相关标签:
3条回答
  • 2020-12-16 01:14

    I suggest you go read about cookies from a browser perspective. It's very interesting. Note that 'max-age' is the newer version of 'expires' and some browsers may not support it. (For example, IE6, IE7, and IE8 don't support max-age.)

    0 means delete the cookie right now.

    negative means preserve the cookie for a while. I suspect the scope is somewhat browser specific but it will not preserve the cookie across closing and reopening the browser.

    0 讨论(0)
  • 2020-12-16 01:19

    setMaxAge(int) as mentioned by Oracle setting max age 0 will delete the cookie instantly. While -1 will delete the cookie when the browser is closed.

    0 讨论(0)
  • 2020-12-16 01:21

    Setting setMaxAge to 0 will delete the cookie. Setting it to -1 will preserve it until the browser is closed.

    0 讨论(0)
提交回复
热议问题