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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

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
RSchnabelAuthor Commented:
We worked around the issue by using mod_usertracks
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.