Meaningless Drivel is fun!*
The moose likes Servlets and the fly likes How to check if cookie is enabled or not Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to check if cookie is enabled or not" Watch "How to check if cookie is enabled or not" New topic
Author

How to check if cookie is enabled or not

Ashutosh Uprety
Ranch Hand

Joined: Nov 30, 2000
Posts: 39
I want to know whether the client browser is having its cookies as enabled or disabled. I have to do this in the code.
Is there any way to do this in JSP or normal javascript/HTML
Mohamed Yousuff
Ranch Hand

Joined: Jun 23, 2001
Posts: 73

I hope that there is only way for this. The solution is to call a servlet will set cookies. The servlet output, the HTML page should immediately refresh to some servlet which will try to retrieve the cookie set by the previous servlet. If it is able to retrieve the cookie, then the client browser accepts cookie else not.
In this regard, let me tell you that there are two types of cookies, one is persistent cookies and the other is non-persistent cookies. This could be seen better with netscape navigator. Session maintenance with getsession() method uses non-persistent cookies. setCookie() method uses persistent cookies. The solution I gave above is for persistent cookies. You can use the same solution but use the getsession() method to test for non-persistent cookies. Hope you understood the solution.
Originally posted by Ashutosh Uprety:
I want to know whether the client browser is having its cookies as enabled or disabled. I have to do this in the code.
Is there any way to do this in JSP or normal javascript/HTML


------------------
Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477
I have tried something like that.... Waiting for the server to start up in debug mode. Let's see what happens!
jamil lusa
Ranch Hand

Joined: Aug 18, 2011
Posts: 59
i could not understand to check the cookie is enabled or not is quite complicated. why the http header or request object does not contain those information?

i would like to know how to check as well, currently i only know how to check that by using javascript. but i prefer to use servlet...

any ideas???
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

Set a cookie, then read it back on the next request. If it's there, cookies are enabled.

And no, there is no header to inform us whether cookies are enabled or not.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
Don't get me started about those stupid light bulbs.
 
subject: How to check if cookie is enabled or not
 
Similar Threads
Session attributes get lost on Firefox
Enabling batch updates in hibernate
MyProfile - can't expand preferences
Hour 17:Creating Interactive Web Programs - Java in 24 hours
To check for javascript