0

I have two war deployed on my web server lets say A.war and B.war & my web application is combination of both the war i.e A and B.war

Now I want to generate the cookies on java side of A war services and want to get the same cookie while I m accessing serivces of B war.

I tried it by using

response.addCookie(new Cookie("key","value"))

in A.war service. Definaltly it will get stored in cookie

& I am getting the cookie in B.war serices as request.getCookie()

do I need to do anything extra apart from this?

ashishl
  • 201
  • 5
  • 12
  • This is only happening because the cookie is being set in the HTTP response and another request is being sent to B from the same client. This kind of smells wrong, but for sharing cookies this *will probably work* – christopher Sep 14 '16 at 18:14
  • could you please give any link or example where cookie can be share from java side itself. – ashishl Sep 14 '16 at 18:16
  • Define shared. Cookies are a HTTP construct so keep that in mind too. – christopher Sep 14 '16 at 18:17
  • http://www.fwd.at/tomcat/sharing-session-data-howto.html will try this one – ashishl Sep 14 '16 at 18:33

2 Answers2

0

Cookies by default are per domain. Normally, load balancer will be having the public URL and web servers will be behind it serving the static content. Application server like for Java will either be behind load balancer directly or through web server. So Essentially as long as all deployed applications are hosted on same domain, Browser will by default send all the cookies to all applications. So yes in your case it will work.

dvsakgec
  • 3,514
  • 4
  • 28
  • 35
  • As I understand setPath("/") will gives you the cookie data in all wars servers which are hosted on same web server. But what will happen if my A war is on some different server and B war is on different server.. Mean how it would work on cluster/node ? Will it work as expected ? – ashishl Sep 15 '16 at 14:14
0

Cookies are returned to any host that matches the cookie domain attribute if any1, otherwise just to the host that set it. If you don't use Cokie::setDomain, the attribute domain is not set.

Furthermore the user-agent will send the cookie along the only requests that have a path that is a sub-directory of the path attribute. If you don't use Cokie::setDomain the path attribute is automatically set to the request path.

So to share a cookie among contexts in the same host you just need to set the path attribute:

Cookie c = new Cookie("name", "value");
c.setPath("/");

request.addCookie(c);

1 Supposed the domain attribute is not rejected.

Community
  • 1
  • 1
Margaret Bloom
  • 41,768
  • 5
  • 78
  • 124