简体   繁体   中英

How to set httpOnly flag true for cookie in Java?

I tried the setHtpOnly(true) option but it doesn't seem to be working as it isn't a method in Cookie. I did see a few solutions using path but I'm not sure about that either.

This is my code.

Cookie cookie = new Cookie (name, entity);
cookie.setPath("/");
httpServletResponse.addCookie(cookie);

What can I do to set the httpOnly flag as true for the above code?

What version of Java are you using? Are you seeing a specific error on compilation? This method seems to exist in Java EE since at least 6 and Servlet 3.1:

https://docs.oracle.com/javaee/6/api/javax/servlet/http/Cookie.html#setHttpOnly(boolean)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM