Meaningless Drivel is fun!*
The moose likes Servlets and the fly likes switching between cookies and url rewritten, HELP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "switching between cookies and url rewritten, HELP" Watch "switching between cookies and url rewritten, HELP" New topic
Author

switching between cookies and url rewritten, HELP

Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3476

Hi all.
In my web application (a book store), I have a servlet that add a specific item to the shopping cart.
for session tracking, I have used the HttpSession object for the servlets and JSPs.
you know like:
session.setAttribute("cart", "some item");
and the link that invokes this servlet is :
----------------
<a href="/store/add?itemId=<%= rs.getString("isbn") %>">Add To Shopping Cart</a>
----------------
every thing work fine, but when disableing the cookies, a NullPointerException is thrown.
can I detect if the user is disabling the cookies ? and if disabling them
How to switch to url rewrite, and if switch, how to keep tracking the user ?
(I mean, every url will have a jsessionid and a long id, and if the user clicks View Shopping Cart, then how my JSP can benefit from the jsessionid number, how to extract it ?)
please help me, coz this subject is very important to me...
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

You should not need to care if a user has disabled cookies (unless you want to explicitly set a 'C'ookie. There is no need to 'switch' to using cookies or url rewriting, as there is a method that takes care of this for you.

All you need to do, to enable URL Rewriting (at least in some containers) is to make sure any and all links (and form actions) are sent through the response.encodeURL() method.

So your HTML would be:
<a href="<%= response.encodeURL("/store/add?itemId=" + rs.getString("isbn")) %>">Add To Shopping Cart</a>
That is *it*. The jsessionid that is inserted into that URL does not need to get extracted by you. The container does that for you, and you access the Session in the normal way. If the user has cookies enabled, the container will not re-write the URL, so that's why you don't need to worry about 'switching' yourself.
[ March 31, 2004: Message edited by: Mike Curwen ]
Ram Prasadh Anandaraj
Greenhorn

Joined: Feb 19, 2004
Posts: 4
Hello, I am not able to replicate the NPE, can you guys give us the exact scenario and the code.
thanks,
prasadh
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: switching between cookies and url rewritten, HELP
 
Similar Threads
URLs + Session Tracking
How To Convert a Servlet File to a JSP File
relating cookies & sessions
Cookies vs. Sessions .....Need an educated choice
Why doesn't exercise 8-1 from Murach's Java Servlets work for me on IE7?