When I retrieve the cookies in my java servlet, all of the values from getPath() are null.
So if a cookie with the same name is set in directory /foo, and at the root directory, I retrieve two cookies with the same exact name, but I can't differentiate them because getPath() returns null for both.
I looked in firebug and saw that firefox was not sending anything for the path.
My application uses 开发者_C百科a "rememberme" cookie with the path set to "/". Everything works fine as long as there is only one cookie with name rememberme. But if somehow another cookie gets set with the same name on a different path like /foo, then my application won't know which one is the one I set for the root.
How can I differentiate the cookies? Do I need to worry about a cookie existing with the same name in a subdir, or can I just assume there will be only the one I set?
If the browser doesn't send a path, you should set the path to "/" in your Cookie handler.
Your server sets the cookies, not the web browser, so if you set all the paths for the cookies that you create to "/" for the same domain, you don't have to worry about it.
I'm not sure how much this will help you but I recently wrote this method to retrieve cookies from a URLConnection object and return them as a string:
public String getCookies(URLConnection connection) {
String headerName = null;
String cookie = "";
for (int i=1; (headerName = connection.getHeaderFieldKey(i))!=null; i++) {
if (headerName.equals("Set-Cookie")) {
if (cookie.equals("")) {
cookie = connection.getHeaderField(i);
}
else {
cookie = cookie + "; " + connection.getHeaderField(i);
}
}
}
writeToCookiesFile(cookie);
return cookie;
}
This method was being used in just a plain application though :) Hope it's of some benefit!
The browser will send cookies defined for path /foo only when the path of the url starts with /foo. If a cookie with the same name is set on both / and /foo, there is no way to distinguish them.
精彩评论