Java HttpCookie setMaxAge() 方法

2025 年 3 月 22 日 | 阅读需 2 分钟

Java HttpCookie 类的 setMaxAge(long expiry) 方法用于以秒为单位设置 Cookie 的最大生存时间。

结果为正值表示 Cookie 将在指定的秒数后过期。

结果为负值表示 Cookie 不会持久存储,将被删除。

 

语法

参数

上述方法只包含一个参数

  1. expiry - 表示一个整数,指定 Cookie 的最大生存时间(秒)。如果将年龄设置为 0,则 Cookie 应立即丢弃,否则 Cookie 的最大生存时间不确定。

返回

不适用。

示例 1

输出

The maximum age of the cookie is given as: 5000

示例 2

输出

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

示例 3

输出

The maximum age of the cookie is given as: -1
下一个主题Java HttpCookie