File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes How to find that Cookies are supported by the browser or not Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to find that Cookies are supported by the browser or not" Watch "How to find that Cookies are supported by the browser or not" New topic
Author

How to find that Cookies are supported by the browser or not

Awais Bajwa
Ranch Hand

Joined: Jan 16, 2001
Posts: 191
Hi Folks,
How we can find that Cookies are supported by the browser in servlet?

Awais
Vicky Pandya
Ranch Hand

Joined: Dec 16, 2004
Posts: 148
here is some info for you of how session works.

you do have to tell container that you want to create or use a session, but container takes care of generating the session ID, creating new Cookie object, stuffing the session ID into the cookie; and setting the cookie as part of the response.

Container tries to use cookies first. If cookies are disabled container falls back to URL rewriting, but you will have to do extra work of encoding all the URLs.

Keep in mind, if the container doesn't get a session Id from the client, container wouldn't know that this is the next request from the client. The container won't have any way to know that it tried cookies the last time, and they didn't work. THE ONLY way the container can recognize that it has seen this client before is if the client sends a session ID.

Now the next request from this same client, it will have the sessionID appended to the request URL, but if the client accepts cookies, the request will also have a session id cookie. When servlet calls request.getSession(), the container reads the session Id from the request, find the session, and thinks to itself that this client accepts cookie so it ignores request.encodeURL() calls, or else it will use URL rewriting.

You as a developer you shouldn't be bothered whether client has disabled cookies or not, as container by itself will fallback to URL rewriting.

HTH,
dema rogatkin
Ranch Hand

Joined: Oct 09, 2002
Posts: 294
The easiest way can be trying a cookie in JavaScript. If you can't use JavaScript then you can display a probe page with some token in all links and request a session creation. If at navigation from this page you will see freshly created session, then it's more likely browser's cookies are disabled.


Tough in space?, <a href="http://tjws.sf.net" target="_blank" rel="nofollow">Get J2EE servlet container under 150Kbytes here</a><br />Love your iPod and want it anywhere?<a href="http://mediachest.sf.net" target="_blank" rel="nofollow">Check it here.</a><br /><a href="http://7bee.j2ee.us/book/Generics%20in%20JDK%201.5.html" target="_blank" rel="nofollow">Curious about generic in Java?</a><br /><a href="http://7bee.j2ee.us/bee/index-bee.html" target="_blank" rel="nofollow">Hate ant? Use bee.</a><br /><a href="http://7bee.j2ee.us/addressbook/" target="_blank" rel="nofollow">Need contacts anywhere?</a><br /><a href="http://searchdir.sourceforge.net/" target="_blank" rel="nofollow">How to promote your business with a search engine</a>
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Set a cookie oan the response, see if you can read it back. If you can, cookies are supported.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
dema rogatkin
Ranch Hand

Joined: Oct 09, 2002
Posts: 294
Bosun Bello
You solution is much better. However when you do cookie testing you should aware that browser can handle session and permanent cookie differently.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How to find that Cookies are supported by the browser or not
 
Similar Threads
Finding all cookies
how to know the Status of the cookies
enable cookies
to find out the browser
Cookies