开发者

How to persist a cookie?

开发者 https://www.devze.com 2022-12-31 12:03 出处:网络
I am creating a cookie in a jsp script, which is located at: www.myproject.com/login/index.jsp if I restart the browser and navigate there, all works well, I can see the cookie persist. If I naviga

I am creating a cookie in a jsp script, which is located at:

www.myproject.com/login/index.jsp

if I restart the browser and navigate there, all works well, I can see the cookie persist. If I navigate to:

www.myproject.com

I am not seeing the cookie. Do I nee开发者_如何学Pythond to set something in the cookie path or domain to make the cookie visible to the entire [myproject.com] domain (I just want to access the cookie from whatever sub path the user may be on). I am creating the cookie like:

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

Thanks


You indeed need to set the cookie path. This defaults to the current path. You can do that by Cookie#setPath().

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


By default, cookies are returned only to the host that saved them.

try to set a domain to a cookie

Check Out: http://docstore.mik.ua/orelly/java-ent/servlet/ch07_04.htm

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号