It's not a secret anymore!
The moose likes JSP and the fly likes Why Cookie.getDomain() returns null? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » JSP
Reply Bookmark "Why Cookie.getDomain() returns null?" Watch "Why Cookie.getDomain() returns null?" New topic
Author

Why Cookie.getDomain() returns null?

Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1865
Hi all,
I'm trying to get all cookies via JSP. I get all the cookies' name and value but I can't get domain and path via cookie.getDomain() and cookie.getPath(). Both of them returns me null.
Why I don't get domain and path which I set for the cookie? I am setting domain and path via cookie.setDomain() and cookie.setPath() methods when I set the cookie initially.
Thanks!
Maulin


1. Have fun @ http://faq.javaranch.com/java/JavaRaq
2. Looking for simple infix2postfix conversion and postfix evaluation package? Click here
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1865
So, I guess nobody knows
Well, I have my reasoning for the behavior but it would be worthless to discuss with myself
Regards
Maulin
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56230
    
  13

Or perhaps your reasoning might jog someone's memory, or perhaps even help future readers of this topic. No?
bear


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1865
I found the answer...I found the answer...
This happens because Kookie API's implementation Kookie.java doesn't return us the Domain, Path, Max Age any extra information except Kookie name,value
NOTE: I have replaced "c" from cookie to "k" everwhere to avoid the posting error I was getting...
Here is a piece of code from Kookie.java,

Here what happens is- browser returns the kookie as defined per RFC 2109
Now, the webserver's code parses the kookie header separated by ";" and so gets following in the list,
name1=value1
domain=domain1
path=path1
(and other parameters like Secure, Comment etc...)
name2=value2
domain=domain2
path=path2
(and other parameters like Secure, Comment etc...)
and then tries to create Kookie object for each of such Name,Value pair after further separting by '=' sign. So, it does,
Kookie tempKookie = new Kookie(name,value);
now the Kookie.java ignores the kookie creation if the name is Path, Domain etc (as per the above Kookie.java) and so it only gets kookie's name and value...
Here is the Tomcat405's code that parses the Kookie. If we combine the knowledge of the RFC 2109, this code and Kookie.java then we would realize what happens here...

hope this is helpful...
and the reason that Kookie.java ignores Domain, Path etc could be just that it wanted to avoid providing any more information to the server as a prevention of possible hack by some other servers. e.g.
- I have a server1, server2.
- server1 sets a kookie called "server1login" w/ domain/path etc..
- server2 hacks the user system's browser and write a code to read kookies that allows it to read "all" kookies set in the browser
- now, if the reading of the kookie returned every bit of information about the kookie then server2's code would know domain/path of server1 and can then overwrite the kookie BUT if it didn't get the domain/path then it won't be able to overwrite the kookie and the user's application runnin in the browser is less liable to mis-behave due to the hack.
- Here server2 CAN override Kookie API defined by Kookie.java and return all the information to the user but that would violate Kookie API but to do that we have to modify server2's servlets.jar where the Kookie.class is there and all those things which can't be just done with a blink of eye....
- if server1's code wanted to modify the kookie (in case of logout if we want to remove the kookie) then the code has to know the domain/path etc information with which the kookie was set and that way it can do things. AND most probably the code on server1 knows those values...
Its difficult to explain but I'm sure you won't have problem in getting what I am trying to say
Regards
Maulin
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Why Cookie.getDomain() returns null?
 
Similar Threads
change cookie value
Reading Cookies from HttpUrlConnection - Set-Cookie Header
Cookies doubt
setMaxAge(0) doesnt delete cookie?
How to delete a cookie from Request (age old question never answered)