This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JSP and the fly likes Cross-Domain Cookies Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Cross-Domain Cookies" Watch "Cross-Domain Cookies" New topic

Cross-Domain Cookies

sundaresan ganapathy

Joined: Oct 08, 2008
Posts: 23
Hi All,
I am trying to have two webapps such as WebApp1 and WebApp2.

1. I am setting one cookie in WebApp1 in the HttpResponse.
2. How to read the same cookie from HttpRequest at WebApp2?

I know it sounds weired b'coz cookies are specific to websites, we cant access it from different webapp. But i heard of CROSS-DOMAIN cookies which can be shared across multiple webapps.How to implement this requirement using CROSS-DOMAIN cookies.

Please post your suggestions on this....

Note: I am trying this at J2ee webapps

Thanks In Advance,
Lester Burnham

Joined: Oct 14, 2008
Posts: 1337
Are both web apps running on the same (sub) domain? If so, you can set the cookie path and domain; check the Cookie.setPath and Cookie.setDomain methods.

And no, HTTP cookies do not work across domains.
sundaresan ganapathy

Joined: Oct 08, 2008
Posts: 23
Hi lester,
I can implement this Domain and SubDomain, can you please provide more clarity on this???

sundaresan ganapathy

Joined: Oct 08, 2008
Posts: 23
I meant, can you please add more clarity on configuration part, how to create/configure domain and subdomain in j2ee environment???

Lester Burnham

Joined: Oct 14, 2008
Posts: 1337
There isn't anything to configure, really, except for using the Cookie.setDomain method for any cookies you create. If you have web apps at and, then setDomain("") should make the cookie visible to both web apps (not sure about the details, play around to see what works). Reading the cookie spec will probably be helpful, too.
Wendy Gibbons

Joined: Oct 21, 2008
Posts: 1107

i found it really helpful using firefox, and using the "delete single cookies" option to scroll through my cookies to see what the values were
sundaresan ganapathy

Joined: Oct 08, 2008
Posts: 23
I tried having two webapps
(1) ==> here i add cookie in respose as follows

Cookie cookie = new Cookie("namedCookie","test");

(2)>Here i tried to access the cookie as follows, but cant able to access

Cookie[]cks = request.getCookies();
for(int i=0;i<cks.length;i++){
out.print("cookie found"+cks[i].getValue());


Any idea on this?>
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 60764

sundaresan ganapathy wrote: it sounds weired b'coz cookies

Please use real words when posting to the forums. Abbreviations such as "b'coz" in place of "because" only serve to make your posts more difficult to read and less likely to generate useful responses.

Please click this link ⇒ UseRealWords for more information.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: Cross-Domain Cookies
Similar Threads
How to pass Session object between two web apps
passing session values between two web application
Tomcat Compiled Java Beans Clashing
Tomcat Question
How to get another webapp's jndi object from local?