SoFunction
Updated on 2025-04-07

A brief discussion on the life cycle of cookies

Set the valid time of the cookie object, setMaxAge() method can set the valid time of the cookie object.

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

(60);//The meaning of 60 seconds

(60*60);//One hour

(365*24*60*60);//One year

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

This cookie whose lifetime is browsing session is called a session cookie. Session cookies are generally not stored on the hard disk but in memory.

If the expiration time is set, the browser will save the cookies to the hard disk, close it and open the browser again. These cookies will still be valid until the set expiration time has exceeded. Cookies stored on the hard drive can be shared between different browser processes, such as two IE windows. Different browsers have different ways to handle cookies stored in memory.

When set to 0, the specified cookie will be deleted on the browser immediately.

When set to -1, it means that closing the current browser will be invalid.

The above brief discussion on the life cycle of cookies is all the content I share with you. I hope you can give you a reference and I hope you can support me more.