简体   繁体   English

Cookie域和路径与jax-rs

[英]Cookie domain and path with jax-rs

I'm using jax-rs and trying to add a cookie to my response. 我正在使用jax-rs并尝试在我的回复中添加一个cookie。 The problem is that when adding a cookie the following way: 问题是,以下列方式添加cookie时:

new NewCookie("cookie-name", "cookie-value");

the cookie is being attached only to the requests of the same path. cookie仅附加到同一路径的请求。 For example, if I add the cookie in the request "/myapp/users/login", I don't see that cookie when calling other requests. 例如,如果我在请求“/ myapp / users / login”中添加cookie,则在调用其他请求时我看不到该cookie。 I think the explanation to this is that, for some reason, the path of the cookie is "/myapp/users" so when calling "myapp/someotherpath" the cookie is not being attached. 我认为对此的解释是,由于某种原因,cookie的路径是“/ myapp / users”所以当调用“myapp / someotherpath”时,cookie没有被附加。

I tried using another constructor of NewCookie where I can set domain and path but couldn't make it work, can some one give me an example of setting domain and path that will attach the cookie to all requests? 我尝试使用NewCookie的另一个构造函数,我可以在其中设置域和路径,但无法使其工作,有人可以给我一个设置域和路径的示例,将cookie附加到所有请求吗? isn't this supposed to be the default behavior anyway? 这不应该是默认行为吗?

To attach the cookie to all requests the domain and path should be: domain = ""; 要将cookie附加到所有请求,域和路径应为:domain =“”; path = "/" path =“/”

So the cookie creation will look as follows: 因此,cookie创建将如下所示:

NewCookie("cookie-name", "cookie-value", "/", "", "comment", 100, false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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