*
The moose likes Servlets and the fly likes cookie setDomain problem. 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 » Servlets
Bookmark "cookie setDomain problem." Watch "cookie setDomain problem." New topic
Author

cookie setDomain problem.

Sanjay Anand
Ranch Hand

Joined: Apr 03, 2001
Posts: 42
hi all,
i am trying to set a session cookie.
the cookie seems to set fine. a java servlet sitting on the same port (tomcat port 8080) can retrieve the cookie. and turning the warning on gives the correct warning for the cookie being set.
and, yet, when it redirects to any other page on our site, the cookie doesn't show up. again, the retreiving servlet can retreive those cookies set by other pages/processes on our site but not the other way around.
i attach the code below.
any suggestions?
thanks in advance.
sanjay


[This message has been edited by Sanjay Anand (edited April 23, 2001).]
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

Hi:
While I don't quite understand the onload tags (javascript I assume), I think you need to use an encoded URL (instead of the
savedurl). Please refer to Marty Halls' book on Core
Servlets and JSP's (specifically pp 206-207 Topic: Encoding URL's Sent to the Client).
out.println("<META http-equiv='refresh' content='0; URL=" + savedurl +"'>\n");
out.println("</NOSCRIPT>\n");
out.println("<BODY
onLoad=\"window.location.replace('"+savedurl+"')\">\n");

As I said, I am not 100% sure of the tags you are using.
regds.
- satya

Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Sanjay Anand
Ranch Hand

Joined: Apr 03, 2001
Posts: 42
the redirecting is not a problem. the problem is that the cookie is not being retreived by any other pages of our site.
thanks for your thoughts, though.
sanjay
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Sanjay,
From Sun's site ,
"By default, cookies are only returned to the host which saved them."
Just to make a test, can you comment your
newCookie.setDomain (".ourserver.domain.com"); code alone and verify?
http://java.sun.com/products/servlet/2.1/api/javax.servlet.http.Cookie.html#setDomain(javax.servlet.http.String)

regds
maha anna
Sanjay Anand
Ranch Hand

Joined: Apr 03, 2001
Posts: 42
Thanks for the reply Maha Anna. I have tried to comment that line and the result is the same.
Indeed, you are correct about the fact that cookies are returned only to the host which saved them. But the documentation also says:

a domain name name begins with a dot (".foo.com") and means that hosts in that DNS zone ("www.foo.com", but not "a.b.foo.com") should see the cookie. By default, cookies are only returned to the host which saved them.

I take this to mean that if the domain name where the servlet resides, in my case, is something like "www.foo.com:8080/servlet", it should be accessible to others in the 'DNS zone' mentioned in the documentation. i.e. all the pages in "www.foo.com". In other words, it should be possible to 'diminish' the dns zone if not transform it.
Doesn't that make sense? If not, what's the point of setting cookies anyway?
Sanjay
manav kher
Ranch Hand

Joined: Nov 21, 2000
Posts: 98
well any www address goes to the default port (www would mean port 80) so www.foo.com is foo.com:80 and is diff from say foo.com:8000.
Sanjay Anand
Ranch Hand

Joined: Apr 03, 2001
Posts: 42
i'm not sure i understand the thrust of what you're saying, manav.
the simple question here is: is it possible to set a domain that can be accessible to other pages in the same site? other pages, that is, that are not running on the tomcat or jrun server that is being used.
and, if so, then what am i doing wrong? why is it not happening?
again, i'd appreciate any help.
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Sanjay,
I made a small test for you. Turns out that, we may have to write cookie.setPath("/") , to make the browser to return this cookie for all resources under "/" in the same server from where the cookie originated. In other words, if "sevletA" in http://localhost:8080/MISApp/servlet/ServletA sets a cookie named "cookieA", in order to make this "cookieA" to be accessed in "http://localhost:8080/MISApp/Login/showCookies.jsp" , we have to write cookie.setPath("/") in the cookie creation/setting code in "ServletA".
Here is the code which I tested. You also cut and paste this code and test to make sure if ServletA sets a cookie, it can retrived from another jsp in this server. Since I am able to view those cookies from showCookies.jsp, I would like you also to make the test, so that we can continue from there on.
If you also succeed in this test, then try to make those "setPath("/") change in your code. Give it a try!
[ Copy SetCookies.class file in your ...WEB-INF/classes dir and showCookes.jsp in your jsp dirs where you can call this jsp. You may have to change the "href link" code in SetCookies.java file accordingly. ]
regds
maha anna
Sanjay Anand
Ranch Hand

Joined: Apr 03, 2001
Posts: 42
Dear maha anna,
I can see now why you have so many fans. Thanks so much for taking the trouble to post the code. And the code works as, actually, did mine. I added the suggested line [cookie.setPath("/")] to my code as well.
In my case I wrote a simple cookie-reading servlet rather than a jsp. But the idea was the same. It too sat in something like http://localhost:8080/MISApp/Login/showCookies.jsp.
But the critical question for us is whether we can see this cookie in http://localhost/anotherpage.html
i.e. can it be accessed by a page *not* sitting on the 8080 portal?
that is something i have not been able to make work yet.
I will keep working on it and would, again, appreciate further thoughts.
Thanks again.
Sanjay
Sanjay Anand
Ranch Hand

Joined: Apr 03, 2001
Posts: 42
YES IT CAN!!
maybe it was the setPath command that did the trick but it's working now.
thankyouthankyouthankyouthankyouthankyou
i love this board
sanjay
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Could see your happiness! and also glad that we finally made it.
regds
maha anna
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: cookie setDomain problem.
 
Similar Threads
basic cookie Doubts
if (req.getMethod().equals("HEAD")) return
how to set Persistant Cookies
Cookie code...
SESSION TRACKING.