tanvi kulkarni
tanvi kulkarni

Reputation: 17

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?

Upvotes: 0

Views: 973

Answers (1)

rixar
rixar

Reputation: 11

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)

Upvotes: 1

Related Questions