Java 在根路径上设置 cookie

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16882394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:15:48  来源:igfitidea点击:

Java setting cookie on root path

javacookies

提问by coure2011

I have a login page at myhost:8080/auth/loginafter I logged in I set a cookie by my home controller class which is at 'myhost:8080/home/

我登录myhost:8080/auth/login后有一个登录页面,我通过我的家庭控制器类设置了一个 cookie,它位于'myhost:8080/home/

I am setting my cookie like this

我正在像这样设置我的 cookie

 Cookie myCookie =  new Cookie("__JSSESSIONID", request.getSession().getId());
 myCookie.setPath("/");
 response.addCookie(myCookie);

but when I see it via firebug, it shows me the path "/home". I want to set cookie on root path so that can ready from other places.

但是当我通过萤火虫看到它时,它向我显示了路径“/home”。我想在根路径上设置 cookie,以便可以从其他地方准备好。

回答by Chris

Here is what you need:

这是您需要的:

<Context ... sessionCookiePath="/" > ... </Context>

Check tomcat documentationfor more details

查看tomcat 文档以获取更多详细信息