JSP - Cookie not Writing to PC

I am trying to write a simple cookie to track return customers below is the code that I am using. It does not seem to be writing the cookie. I have checked the browsers to make sure that cookies are allowed. There are no errors generated, but it does not seem to do anything. Any suggestions are welcome:

Date now = new Date();
String CookieValue = request.getRemoteAddr() +  now.toString();
boolean CookieFound = false;
Cookie[] cookies = request.getCookies();
for(int i = 0; i < cookies.length; i++) {
     if (cookies [i].getName().equals ("status")){
      CookieFound = true;
      break;
     }
}  
if (!CookieFound) {
     Cookie cookie = new Cookie ("status",CookieValue);
     cookie.setMaxAge(365 * 24 * 60 * 60);
     response.addCookie(cookie);
}
RSchnabelAsked:
Who is Participating?
 
RSchnabelConnect With a Mentor Author Commented:
We worked around the issue by using mod_usertracks
0
 
mrcoffee365Commented:
Since you don't set the path for this cookie, it might have been created, but you aren't seeing it because you aren't checking it in the same path.

Try setting
cookie.setPath("/");
before you call addCookie and see if the cookie appears the way you expect.

You should set your preferences in your browser to ask you every time about setting cookies -- that way it's easier to see if a cookie is being set.
0
 
RSchnabelAuthor Commented:
Thank you for the suggestion. I did set the path, but it did not fix the issue. Also I was pretty sure that was not the issue because I had looked at all of the cookies for the domain and there was only the session id. Any other ideas?
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
mrcoffee365Commented:
How are you checking for cookies?  The cookie is being created on the server, and being sent to the browser.   However, until the browser returns the cookie to the server, the server won't read it in the request cookies.

If you're using Firefox, there's a great utility called LiveHTTPHeaders which will show you the request and response streams from/to the browser.  If you run that, then run a test where you set the cookie as you do above, then you'll see the cookie being sent from the server to the browser in the response.

The cookie.setPath makes a difference, of course, because without it, the cookie is only available at /path-where-cookie-was-created/pages on your site.
0
 
RSchnabelAuthor Commented:
I am going to tool, options, then clicking on view cookies. I then scroll down to my domain and see what cookies are there
0
 
mrcoffee365Commented:
That sounds right.

When I run a jsp page using your code above, the cookie named "status" is created and shows in the View Cookies list.

So I think there is something preventing your addCookie code from running.  

Have you tried a test where you create a single page from your code, and see if it sets a cookie?  That might help you limit your debugging, and give us more of a chance to help you.

Otherwise, if your code is fairly short, then please post the whole JSP page.
0
 
mrcoffee365Commented:
Okay.  It's too bad you didn't track down the problem with creating cookies -- whatever is preventing your code from running successfully might affect something else.

But it's good you found a workaround.  Since you didn't get a solution here, you can ask in Community Support to get your points refunded.
0
All Courses

From novice to tech pro — start learning today.