The moose likes Servlets and the fly likes getPath() & setPath() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "getPath() & setPath()" Watch "getPath() & setPath()" New topic

getPath() & setPath()

Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
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,
& 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).
Les Dsouza

Joined: Jan 29, 2002
Posts: 27
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:
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
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.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: getPath() & setPath()