On the life cycle of Cookie

  • 2021-07-07 06:06:18
  • OfStack

By setting the effective time of the Cookie object, the setMaxAge () method sets the effective time of the Cookie object,

For example: Cookie c = new Cookie ("username", "john");

c. setMaxAge (60); //60 seconds

c. setMaxAge (60*60); //1 hour

c. setMaxAge (365*24*60*60); //1 year

If the expiration time is not set, it means that the life cycle of cookie is during the browser session, and cookie disappears as long as the browser window is closed.

This cookie whose lifetime is the browsing session is called the session cookie. Session cookie1 is not saved on the hard disk but in memory.

If the expiration time is set, the browser will save the cookie to the hard disk, close it and open the browser again. These cookie will remain valid until the set expiration time is exceeded. The cookie stored on the hard disk can be shared between different browser processes, such as two IE windows. For cookie stored in memory, different browsers have different processing methods.

When cookie. setmaxage is set to 0, the specified cookie is immediately deleted from the browser

When cookie. setmaxage is set to-1, it means that closing the current browser is invalid.


Related articles: