Httponly cookie is a cookie security solution.
In browsers that support httponly cookies (IE6, FF3.0), if the "httponly" attribute is set in the cookie, the JavaScript script will not be able to read the cookie information, which can effectively prevent XSS attacks and allow the website to The application is more secure.
But J2EE4 and J2EE5 cookies do not provide a method to set the httponly attribute, so if you need to set the httponly attribute, you need to handle it yourself.
import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletResponse; /** * Cookie Tools */ public class CookieUtil { /** * Set httponly cookie * @param Response HTTP response * @param Cookie cookie object * @param Ishttponly is httponly */ public static void addCookie(HttpServletResponse response, Cookie cookie, boolean isHttpOnly) { String name = cookie.getName();//Cookie name String value = cookie.getValue();//Cookie value int maxAge = cookie.getMaxAge();//Maximum survival time (milliseconds, 0 representative deletion, -1 represents the same as the browser session) String path = cookie.getPath();//path String domain = cookie.getDomain();//area boolean isSecure = cookie.getSecure();//Is there a security protocol? StringBuilder buffer = new StringBuilder(); buffer.append(name).append("=").append(value).append(";"); if (maxAge == 0) { buffer.append("Expires=Thu Jan 01 08:00:00 CST 1970;"); } else if (maxAge > 0) { buffer.append("Max-Age=").append(maxAge).append(";"); } if (domain != null) { buffer.append("domain=").append(domain).append(";"); } if (path != null) { buffer.append("path=").append(path).append(";"); } if (isSecure) { buffer.append("secure;"); } if (isHttpOnly) { buffer.append("HTTPOnly;"); } response.addHeader("Set-Cookie", buffer.toString()); } }
It is worth mentioning that the cookie in Java Ee 6.0 has set httponly, so if it is compatible with a Java EE 6.0 compatible container (such as Tomcat 7), you can use cookie.sethttponly to set HTTPONLY:
cookie.setHttpOnly(true);
The setHttpOnly(Boolean httpOnly) method of the Java HttpCookie class is used to indicate whether the cookie can be considered HTTPOnly. If set to true, the cookie cannot be accessed by scripting engines such as JavaScript.
Syntax
public void setHttpOnly(boolean httpOnly)
Scope
The above method requires only one parameter:
httpOnly - true if the cookie is HTTP only, which means it Visible as part of the HTTP request.
Return
Not applicable
Example 1
import java.net.HttpCookie; public class JavaHttpCookieSetHttpOnlyExample1 { public static void main(String[] args) { HttpCookie cookie = new HttpCookie("Student", "1"); // Indicate whether the cookie can be considered as HTTP Only or not. cookie.setHttpOnly(true); // Return true if the cookie is considered as HTTPOnly. System.out.println("Check whether the cookie is HTTPOnly: "+cookie.isHttpOnly()); } }
Output:
Check whether the cookie is HTTPOnly: true
Example 2
import java.net.HttpCookie; public class JavaHttpCookieSetHttpOnlyExample2 { public static void main(String[] args) { HttpCookie cookie = new HttpCookie("Student", "1"); // Indicate whether the cookie can be considered as HTTP Only or not. cookie.setHttpOnly(false); // Return false if the cookie is not considered as HTTPOnly. System.out.println("Check whether the cookie is HTTPOnly: "+cookie.isHttpOnly()); } }
Output:
Check whether the cookie is HTTPOnly: false
Example 3
import java.net.HttpCookie; public class JavaHttpCookieSetHttpOnlyExample3 { public static void main(String[] args) { HttpCookie cookie1 = new HttpCookie("Student1", "1"); HttpCookie cookie2 = new HttpCookie("Student2", "2"); //Indicate whether the cookie can be considered as HTTP Only or not. cookie1.setHttpOnly(true); cookie2.setHttpOnly(false); System.out.println("Check whether the first cookie is HTTPOnly:"+cookie1.isHttpOnly()); System.out.println("Check whether the second cookie is HTTPOnly:"+cookie2.isHttpOnly()); } }
Output:
Check whether the first cookie is HTTPOnly:true
Check whether the second cookie is HTTPOnly:false
The above is the detailed content of How to set HttpOnly Cookie in Java?. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Notepad++7.3.1
Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version
God-level code editing software (SublimeText3)
