Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

creation of cookie

Status
Not open for further replies.
Oct 15, 2003
145
US
Why is it when I first load my page that has the following code - that it tells me that Cookies are disabled - yet the cookie property of the navigator object in javascript tells me that cookies are enabled (which is correct - cookies are enabled.).

Then I refresh my page and this code says that cookies are enabled and displays the information that I had requested.

Cookie cookie = new Cookie("TestCookie", "Just a test of ability");
response.addCookie(cookie);
//CHECK TO SEE IF COOKIE WAS CREATED
Cookie[] allCookies = request.getCookies();
Cookie ourCookie = null;
if (allCookies!=null)
{
for (int i=0; i< allCookies.length; i++)
{
if (allCookies.getName().equals("TestCookie"))
{
ourCookie = allCookies;
}
}
}
if (ourCookie == null)
{
out.println("Cookies are disabled.<BR>");
} else {
out.println("Cookies are Enabled<BR>");
out.println("Cookie Version: " + ourCookie.getVersion());
out.println("<BR>Cookie Name: " + ourCookie.getName());
out.println("<BR>Cookie Maxage: " + ourCookie.getMaxAge());
out.println("<BR>");
}
 
Have you tried deleting your browser's cache ?

--------------------------------------------------
Free Database Connection Pooling Software
 
I'm assuming you mean deleting all the cookies and temp files?

If that's what you mean - yes...and still get the same thing.
 
You are only testing the last cookie in the array - I think you should do something more like :

Code:
Cookie cookie = new Cookie("TestCookie", "Just a test of ability");
response.addCookie(cookie);
//CHECK TO SEE IF COOKIE WAS CREATED
Cookie[] allCookies = request.getCookies();
Cookie ourCookie = null;
if (allCookies!=null)
{
    for (int i=0; i< allCookies.length; i++)
    {
        if (allCookies[i].getName().equals("TestCookie"))
        {
            ourCookie = allCookies[i];

	    if (ourCookie == null)
	    {
		out.println("Cookies are disabled.<BR>");
	    } else {
		out.println("Cookies are Enabled<BR>");
		out.println("Cookie Version: " + ourCookie.getVersion());
		out.println("<BR>Cookie Name: " + ourCookie.getName());
		out.println("<BR>Cookie Maxage: " + ourCookie.getMaxAge());
		out.println("<BR>");
	    }             
            
        }
    }
} else {
	out.println("'allCookies' is null !<BR>");
}
 
[code]

--------------------------------------------------
Free Database Connection Pooling Software
[URL unfurl="true"]http://www.primrose.org.uk[/URL]
 
Thank you for your thoughts - sedj...

I put your code in my page and I still have the same problem.

The code you wrote - says --> 'allCookies' is null! however the navigator.cookieEnabled says that cookies are enabled. So they are still conflicting in their answers. when I click on the refresh - then the code that was written above - says that cookies are enabled and shows me the version, name, and maxage.

I don't understand why these two things give me different answers. when I'm expecting them to give the the same answer.
any thoughts?
 
Does it occur on all browsers (IE, Firefox, Opera etc) or just one ?

--------------------------------------------------
Free Database Connection Pooling Software
 
Oh gosh, I've just re-read your code, and realised why it doesn't work ...

OK, the getCookies() method asks to get cookies associated with this session /request object. If there are none associated (ie when you first load the page), then it returns null.

When you call addCookie() to the response object, this will not actually add a cookie (that can be retrieved from the request object) until the page is fully loaded and transmitted back to the client browser.

Once that has occured, the getCookies() will return the cookies you have previously added.

So if getCookies() is the first time does not actually mean cookies are disabled - just that you have not processed the page completely so the browser has not had a chance to store the cookie.

--------------------------------------------------
Free Database Connection Pooling Software
 
Hrm...well what you described is what is happening - so that makes sense. I just wansn't understanding the method completely. Thank you for explaining this.

So is the only way to detect if a person has cookies enabled or disabled - is to use javascript and use navigator.cookieenabled?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top