Java HttpCookie setHttpOnly() 方法

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

Java HttpCookie 类的 setHttpOnly(Boolean httpOnly) 方法用于指示 cookie 是否可被视为 HTTPOnly。如果设置为 true,则 cookie 不能被 JavaScript 等脚本引擎访问。

 

语法

参数

上述方法只需要一个参数

  1. httpOnly - 指示 true,如果 cookie 是 HTTP Only,意味着它仅在 HTTP 请求中可见。

返回

不适用。

示例 1

输出

Check whether the cookie is HTTPOnly: true

示例 2

输出

Check whether the cookie is HTTPOnly: false

示例 3

输出

Check whether the first cookie is HTTPOnly:true
Check whether the second cookie is HTTPOnly:false
下一主题setMaxAge() 方法