Trying to use cookies with a JSP...

Discussion in 'Java' started by Tim O'Connor, Sep 18, 2003.

  1. Tim O'Connor

    Tim O'Connor Guest

    ....and not having a lot of success. I'm using this code that I got
    from a website:

    <%
    Cookie[] cookies = request.getCookies();

    for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++)
    {
    Cookie cookie1 = cookies[loopIndex];
    out.println("name = " + cookie1.getName() + "<BR>");
    out.println("value = " + cookie1.getValue() + "<BR>");
    out.println("domain = " + cookie1.getDomain() + "<BR>");
    }
    %>

    When I run it, it retrieves a single cookie whose name is SITESERVER,
    value is ID=averylonghexnumber and Domain is null. This does not
    resemble any of the cookies in my C:\Documents and
    Settings\OConnorT\Cookies directory (of which there are many). I've
    also tried setting a cookie using:
    <%
    Cookie cookie = new Cookie("MyCookie", "CookieValue");
    response.addCookie(cookie);
    %>

    but when I read it back it can't be found. What am I doing wrong?
    Does it have to do with the cookie Domain? What is the "domain" of a
    cookie? I'm using Windows XP Professional and IE 6.xx.

    Tim
    Tim O'Connor, Sep 18, 2003
    #1
    1. Advertising

  2. Tim O'Connor

    Scott Yanoff Guest

    Tim O'Connor wrote:
    > <%
    > Cookie[] cookies = request.getCookies();
    >
    > for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++)
    > {
    > Cookie cookie1 = cookies[loopIndex];
    > out.println("name = " + cookie1.getName() + "<BR>");
    > out.println("value = " + cookie1.getValue() + "<BR>");
    > out.println("domain = " + cookie1.getDomain() + "<BR>");
    > }
    > %>


    It might be worthwhile to wrap that "for" loop with the following:
    if (cookies != null).

    It's often faster to loop backwards rather than calculating
    cookies.length with each iteration of the loop. I doubt you save many
    milliseconds doing it this way, but ya never know.
    for (int loopIndex = cookies.length - 1; loopIndex >= 0; loopIndex--) {

    Lastly, are you aware that you cannot read the cookies on your
    harddrive. Instead, you are reading the cookies of the user's browser
    that is hitting your JSP. And, you can only read cookies that you
    created from your domain, for obvious security reasons.

    > Settings\OConnorT\Cookies directory (of which there are many). I've
    > also tried setting a cookie using:
    > <%
    > Cookie cookie = new Cookie("MyCookie", "CookieValue");
    > response.addCookie(cookie);
    > %>


    It's usually a good idea to set some other things associated with the
    cookie, such as expires, path, domain, etc. For example, you would want
    to set the path so that the cookie can be read from any part of your
    site. You can limit where the cookie can be accessed if you change
    this, but generally cookies get set to:
    cookie.setPath("/");

    You can limit which machine in your domain the cookie is valid for, or
    specify that it is valid for anything in ".timoconnor.org" with:
    cookie.setDomain(".timoconnor.org");

    HTH,
    --
    -Scott
    | http://www.yanoff.org | AOL IM: SAY KJY
    Scott Yanoff, Sep 18, 2003
    #2
    1. Advertising

  3. Tim O'Connor

    Phil Hanna Guest

    On 18 Sep 2003 13:17:27 -0700, (Tim O'Connor)
    wrote:

    >...and not having a lot of success. I'm using this code that I got
    >from a website:
    >
    ><%
    > Cookie[] cookies = request.getCookies();
    >
    > for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++)
    > {
    > Cookie cookie1 = cookies[loopIndex];
    > out.println("name = " + cookie1.getName() + "<BR>");
    > out.println("value = " + cookie1.getValue() + "<BR>");
    > out.println("domain = " + cookie1.getDomain() + "<BR>");
    > }
    >%>
    >
    >When I run it, it retrieves a single cookie whose name is SITESERVER,
    >value is ID=averylonghexnumber and Domain is null. This does not
    >resemble any of the cookies in my C:\Documents and
    >Settings\OConnorT\Cookies directory (of which there are many). I've
    >also tried setting a cookie using:
    ><%
    > Cookie cookie = new Cookie("MyCookie", "CookieValue");
    > response.addCookie(cookie);
    >%>
    >
    >but when I read it back it can't be found. What am I doing wrong?


    If you want a cookie to persist between browser sessions, you need to
    set its expiration date to a non-zero value. By default, it goes away
    as soon as the HTTP session does.

    final int ONE_DAY = 60 * 60 * 24; // Seconds in one day

    Cookie cookie = new Cookie("MyCookie", "CookieValue");
    cookie.setMaxAge(ONE_DAY);
    response.addCookie(cookie);

    Also, all those cookies in your browser cache are ignored because they
    don't belong to the same domain as the page that's reading them.

    The servlet API javadocs for javax.servlet.http.Cookie describe the
    domain and path parameters you may want to adjust.
    --
    Phil Hanna
    Author of JSP 2.0: The Complete Reference
    http://www.philhanna.com
    Phil Hanna, Sep 20, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page