Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Awais Bajwa
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks,
How we can find that Cookies are supported by the browser in servlet?

Awais
 
Vicky Pandya
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Set a cookie oan the response, see if you can read it back. If you can, cookies are supported.
 
dema rogatkin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic