Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getPath() & setPath()

 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
what is the meaning of setPath() method for Cookies? i tried to understand it but i'm not sure that i got it right or not.
here is what i understood,
when we set a path for some cookie then that cookie is made visible to the path and the child directories recursively. meaning - say if we have following hierarchy,
dir1
-file1
-dir2
--file2
--dir3
---file3
& if we set path of a cookie C to be dir1/dir2 then i can't get the cookie C in file1 but i will be able to get cookie in file3. am i correct?
i hope u got the directory hierarchy (its not drawn properly here).
regards
maulin.
 
Les Dsouza
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When a browser sends a request to a web server it will send the cookies set by that server along with the request.
The path in setPath, getPath refers to the path of the page that saved the cookie on your machine.
For example if the path is www.xyz.com/home/index.jsp then the cookie will be available to all the pages in the folder "home" and its sub folders.

The setPath method can be used to specify something more general.
For example, Cookie.setPath("/") specifies that all pages on the server should receive the cookie.
The path specified must include the current page; that is, you may specify a more general path than the default, but not a more specific one.
For more on cookies, check out the url below:
http://pdf.coreservlets.com/CSAJSP-Chapter8.pdf
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi les,
so my understanding was not wrong about cookies's setPath() and getPath() methods.
well, sorry to ask another question but right now i dont have access to servlet environment and i cant test it writing down a program so i have to ask.
the question arises is,
say i 've a cookie with name "x" visible to dir1 on the server and its subfolders and we also 've dir2 at the same level as dir1. now, can i create cookie with same name "x" in any of the other file in dir2 and set it's path to be dir2 (and its subfolders)??? won't it create name conflict as cookie is gonna b on single dir on the client side?
i will try to run this kind of code as soon as i get access to servlet environment.
thanks!
maulin.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic