views:

257

answers:

4

I want to add two values in cookie and retrieve them. I am doing in this way, but I am getting only the first value, not the second.

Cookie c = new Cookie("a", a);
c.setMaxAge(60);
response.addCookie(c);

Cookie b = new Cookie("d", d);
b.setMaxAge(5 * 60);
response.addCookie(b);

While reading:

Cookie cookies[] = getRequest().getCookies();
Cookie myCookie = null;
if (cookies != null) {      
    for (int i = 0; i < cookies.length; i++) {
        log.info("test ;;;"+cookies[i].getName());
    }
}

This returns only one data.

A: 

I would implement something like:

for(int i= 0; i < cookies.length; i++) { 
   Cookie cookie = cookies[i];
   log.info("name: "  + cookie.getName()) 
   log.info("value: " + cookie.getValue()) 
}

This should print name and values of the cookies. If this is not working, probably the cookies are not added correctly to the response. Check that the cookies length is the one expected.

dawez
+1  A: 

You are likely reading them from the wrong request. The newly added cookies will only be available in the subsequent requests, they will not be reflected immediately in the current request. So if you for instance add a cookie to the response and then tries to read it from the current request (the one associated with the very same response where you added the cookie to), then you won't get the added cookie at all. This also applies when you're forwarding the request from one to other resource (i.e. Servlet or JSP).

Debug/read the request/response headers in the client side as well for the sake that. In FireFox you can use the Firebug for this (open the Firebug pane, go to tab Net, click the request in question and you'll see both the request/response headers, the cookies are in there as well).

BalusC
A: 

You can do some thing like this dear, I have tested it and its working

response.addCookie(new Cookie("name","sunny")); response.addCookie(new Cookie("pwd","sunnymehta"));

Cookie[] cookie=request.getCookies(); for(Cookie ck:cookie) { System.out.println(ck.getName()); }

Sunny
Uh, can you also tell why that would work in OP's case as well? No, don't say that it's because you used an enhanced for loop instead of a standard for loop. This makes no difference.
BalusC
A: 

I would take a look at the actual cookie being saved in your browser. The first thing that comes to mind is the fact that in the underlying file that stores your cookie data, there is actually only one file -- the cookie objects in your code are actually being encoded as name-value pairs in a single file. The article at http://www.quirksmode.org/js/cookies.html has some good detail on how the data is actually stored in the cookie file. (Actually more than name-value pairs, since it also accomodates the other cookie properties like the expiration date and the secure flag, but anyway the article will show you that format.)

I gather that your java calls should be writing a validly formatted cookie file, and generating a valid array of cookie objects for you. But the fact that you're getting one object back seems suspicious to me in light of the underlying data format of the cookie.

In the past I've used Cookie Pal to inspect raw cookie data, though the site mentions IE6 support so I guess it's a little out of date.

Paul