Java – how do I persist cookies?

I create a cookie in the JSP script, which is located at:

www.myproject.com/login/index.jsp

If I restart the browser and navigate there, everything is normal and I can see that the cookie still exists If I navigate to:

www.myproject.com

I didn't see the cookie Do I need to set some content in the cookie path or domain to make the cookie visible to the entire [myproject. Com] domain (I only want to access the cookie from any sub path where the user is located) I am creating cookies, such as:

Cookie c = new Cookie("thisisatest","foo");
c.setMaxAge(60 * 24 * 3600);
response.addCookie(c);

thank you

Solution

You really need to set the cookie path The default is the current path You can finish before cookie #setpath()

Cookie c = new Cookie("thisisatest","foo");
c.setMaxAge(60 * 24 * 3600);
c.setPath("/");
response.addCookie(c);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>