This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
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...
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 ]