views:

87

answers:

4

Hi,

How do I differentiate between multiple cookies set through my site? I am setting two kinds of cookies one to see if the user has visited the site or not and an other one for authentication. How do I differentiate between these two? I get both of them when someone accesses a page after authentication. Do I add extra information to the Cookie Value or is there some other way? I understand the setName() function will change the name (from jsessionid) for every cookie from then on. Am I correct?

  • Pav
A: 

Wrong question. Cookie name is set when the cookie object is created.

+1  A: 

Regardless, to authenticate an user, I'd rather use the HttpSession instead. On login, put the User object as a session attribute so that you can just check the presence of the User object in the session. The HttpSession itself is backed by the JSESSIONID cookie, the only difference is that the servletcontainer will manage this all for you transparently.

BalusC
JSESSIONID is not enough isn't it? I need to differentiate between different cookies and the only way to do that is by setting the name of the cookie per functionality.
You should not touch the JSESSIONID cookie in any way. It's used to maintain the server side `HttpSession`. For the "visited" cookie, just create your own cookie as you already did. To differentiate it on every request, just loop through `request.getCookies()` and check its name.
BalusC
Yes. That is correct. I realized that but the main problem with my code as I debug it now is that the cookie I am setting with addCookie is not reaching the browser. I misunderstood it and thought the name is not getting set when in fact the cookie itself is not reaching the browser. The only fields I set in the cookie are the name, domain and value. I do not see it in the browser somehow. Any guesses on why this could be happening?
Maybe you're looking at the wrong response? How are you checking this all? I've found [Firebug](http://getfirebug) plugin for Firefox very useful in this all.
BalusC
A: 

Look at this site for a cookie tutorial http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Cookies.html

You should be able to do a getName and check the name for the cookie. Here is a sample

  public static String getCookieValue(Cookie[] cookies,
                                      String cookieName,
                                      String defaultValue) {
    for(int i=0; i<cookies.length; i++) {
      Cookie cookie = cookies[i];
      if (cookieName.equals(cookie.getName()))
        return(cookie.getValue());
    }
    return(defaultValue);
  }

You shoud create the cookie with something like this ...

Cookie searchStringCookie =
  new LongLivedCookie("name", value);
response.addCookie(searchStringCookie);
Romain Hippeau
Thanks. I saw that and am getting there.
Guys.. response.addCookie() is not setting the cookie in the browser. Any pointers on why this could be? The only fields I set in the cookie are the name, domain and value. I do not see it in the browser somehow.
@user220201 Are cookies turned off ?
Romain Hippeau
No. Cookies are not turned off. I am seeing some JSESSIONID cookie in the browser.
A: 

Cookie is constructed as a name-value pair.

The getCookies() call of HttpServletRequest interface will return all the cookies in the request at hand.

You can iterate over all the cookies and find your requred cookie by checking the name using the getName call of the Cookie and retrieve it's value.

bdhar