Solved

Servlet Sessions for a Shopping Cart

Posted on 2004-08-04
20
269 Views
Last Modified: 2013-11-29
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
0
Comment
Question by:cartman2000
  • 8
  • 6
  • 3
  • +1
20 Comments
 
LVL 14

Expert Comment

by:sudhakar_koundinya
Comment Utility
use session.setMaxInactiveInterval(interval)

regards
Sudhakar
0
 

Author Comment

by:cartman2000
Comment Utility
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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
Comment Utility
oh sorry
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
Comment Utility
But ur code always gets the new session. It will not look for old session.
0
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 50 total points
Comment Utility
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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
Comment Utility
if you think the cookie is insecured store that session info at database
0
 

Author Comment

by:cartman2000
Comment Utility
I kinda had thought of that, is there no other way without using cookies ??
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
Comment Utility
hmm,
only other thought in my mind as of now is database
0
 

Author Comment

by:cartman2000
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 14

Expert Comment

by:sudhakar_koundinya
Comment Utility
create persistant cookies and try
0
 

Author Comment

by:cartman2000
Comment Utility
any idea how I do that??
0
 
LVL 92

Assisted Solution

by:objects
objects earned 50 total points
Comment Utility
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
 

Author Comment

by:cartman2000
Comment Utility
ok but say I do get the sessionId from the cookie,
How do I connect to that particular session with that Id??
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
all browser windows from the same process will share the same session (at least in IE)
0
 

Author Comment

by:cartman2000
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
> 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
 
LVL 1

Expert Comment

by:keesjanvoogd
Comment Utility

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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
Comment Utility
thanks
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Java 1603 Error 2 42
countX 22 68
strCount chalenge 3 50
strCopies  challenge 17 73
E-commerce is quite a gambling world, and you should never entrust your business to a lucky chance. In order to outrun your competitors in a race to attract as many customers as possible, you need to have a well thought-out strategy under your belt.…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now