Servlet Sessions for a Shopping Cart

Hey experts,

I am setting up a shopping cart and want to set up sessions for it. (for obvious reasons).

I have a simple session mechanism working using code like:

  HttpSession session = request.getSession(true);
  if(session.isNew())
  session.setAttribute("session.basket", basket);
  basket = (Basket)session.getAttribute("session.basket");

But it seems that when a user closes the browser window the session still exits BUT if the user goes to the URL it starts a new session EVEN though the users old session still exists.

So basically my problem is that when the user closes his browser window and goes back to the site later he is not in his old session.

Cheers,
Cartman
cartman2000Asked:
Who is Participating?
 
sudhakar_koundinyaCommented:
request.getRequestedSessionId() might be helpful but that requested session id must be known

So do like this when user navigates first, store the session id at client side as cookie.
when user closes the window and reopens the site, read the cookie and get the requested session id. if the session id exits then create the session for him based on the information available

hope this helps
0
 
sudhakar_koundinyaCommented:
use session.setMaxInactiveInterval(interval)

regards
Sudhakar
0
 
cartman2000Author Commented:
Hey Sudhakar,
I don't see how that could help because I want the user to be able to reconnect to his/her session even if he/she closes the browser

Regards,
cartman
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
sudhakar_koundinyaCommented:
oh sorry
0
 
sudhakar_koundinyaCommented:
But ur code always gets the new session. It will not look for old session.
0
 
sudhakar_koundinyaCommented:
if you think the cookie is insecured store that session info at database
0
 
cartman2000Author Commented:
I kinda had thought of that, is there no other way without using cookies ??
0
 
sudhakar_koundinyaCommented:
hmm,
only other thought in my mind as of now is database
0
 
cartman2000Author Commented:
And now I am having the EXACT same problem with cookies once the user closes the browser he cant connect to the cookie he was using
0
 
sudhakar_koundinyaCommented:
create persistant cookies and try
0
 
cartman2000Author Commented:
any idea how I do that??
0
 
objectsCommented:
not much you can do about that, if you're using cookies for tracking your session then each new browser process will create a new session on the server.
And if you use URL rewriting the window will have its own server session.
0
 
cartman2000Author Commented:
ok but say I do get the sessionId from the cookie,
How do I connect to that particular session with that Id??
0
 
objectsCommented:
all browser windows from the same process will share the same session (at least in IE)
0
 
cartman2000Author Commented:
I am not exactly sure what you meant above, but here me out

The servlet looks for a cookie that contains a sessionId,
If it does NOT find one it creates a new session and a new cookie and attaches the sessionId to that cookie.
If it does find one and gets the sessionId from it is there any way to connect to that particular session

Somthing like:
HttpSession session = request.getSession(SessionId);

Thanks
0
 
objectsCommented:
> I am not exactly sure what you meant above

if you open a new browser window using File>New then it will share the same session.
if you open a new IE process it will use a seperate session
0
 
keesjanvoogdCommented:

very good idea to use a persistent cookie for a shopping basket. Why not try to put price per order in there aswell ?? This sounds stupid but there are commercial (e-)commerce apps that sell as an of the shelf webshop that do just that.

Make your data persistent on the server. If you don't have a database use a file or a singleton to store the data (or objects. Oh and remember to flush your object cache every once in a while).

If people login use a user_id to get and store info. This tends to vary less than the session id.

KJ
0
 
sudhakar_koundinyaCommented:
thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.