• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why Cookie.getDomain() returns null?

 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65518
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or perhaps your reasoning might jog someone's memory, or perhaps even help future readers of this topic. No?
bear
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!