Java HttpCookie setMaxAge() Method

The setMaxAge(long expiry)method of Java HttpCookie class is used to set the maximum age of the cookie within seconds.

The result with a positive value indicates that the cookie will expire after the specified time in seconds.

The result with a negative value indicates that the cookie is not stored persistently and will be deleted.

Syntax

Parameter

The above method consist of only one parameter:

  1. expiry - denotes an integer that specifies the maximum age of the cookie in seconds. If the age is set to 0 then the cookie should be discarded immediately else the maximum age of the cookie is unspecified.

Return

N.A.

Example 1

Test it Now

Output:

The maximum age of the cookie is given as: 5000

Example 2

Test it Now

Output:

The maximum age of the first cookie is given as: 3500
The maximum age of the second cookie is given as: 4500

Example 3

Test it Now

Output:

The maximum age of the cookie is given as: -1
Next TopicJava HttpCookie




Latest Courses