jQuery in Action, 2nd edition*
The moose likes Servlets and the fly likes Cookie persistance across packages? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Cookie persistance across packages?" Watch "Cookie persistance across packages?" New topic
Author

Cookie persistance across packages?

Chad Clites
Ranch Hand

Joined: Aug 16, 2005
Posts: 134
I have an app that checks for a cookie, and if it doesn't exist, asks for log-in information, and sets a cookie once login information is verified.

Nothing earth shattering there, and that works perfectly. The cookie is set. Once the cookie is set, the servlet forwards the response to a servlet in another package. I am trying to use the following code to retrieve the cookie:

If I try to do getC00kies from a different package, cookieArray is null. I know the code works though because if I use it from the same package, then I can get the cookies. I have been reading as many different threads I can find on the topic, but I can't find any that specifically deal with multiple packages, and I am guessing that the fact that I am not able to get the cookies has to do with the fact it is from a different package.

Is that a correct assumption? I have checked Sun's documentation, and I can not seem to find what I have to do to set the cookie to make it available across packages.

EDIT: Now that I asked, I apparently need to set the domain correctly for each cookie....

[ August 23, 2006: Message edited by: Bear Bibeault ]
[ August 23, 2006: Message edited by: Chad Clites ]
Chad Clites
Ranch Hand

Joined: Aug 16, 2005
Posts: 134
I apologize for the double post, but I figured out my problem. My assumption about setting the Domain was correct; however, many threads I looked at were giving incorrect advice. Since I am testing in my development server, I knew my domain was localhost. Many of the threads I looked at recommended setting domain like this:

userCookie.setDomain("localhost");
or
userCookie1.setDomain("http//localhost:8080");

Unfortunately, that advice seems to be incorrect. From the Cookie documentation:
The form of the domain name is specified by RFC 2109. A domain name begins with a dot (.foo.com) and means that the cookie is visible to servers in a specified Domain Name System (DNS) zone (for example, www.foo.com, but not a.b.foo.com).


So it turns out that the correct domain for my cookie was:
userCookie1.setDomain(".localhost.Auth_New");

And now it works perfectly.
[ August 23, 2006: Message edited by: Chad Clites ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cookie persistance across packages?
 
Similar Threads
Not able to set cookies
Cookies not getting reset in Netscape 4.72!!! URGENT!!!
Why isn't my cookie being set?
response.addCookie is not working
unable to retrieve cookies & logout properly