Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Servlet Sessions for a Shopping Cart

Posted on 2004-08-04
20
275 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
ID: 11720216
use session.setMaxInactiveInterval(interval)

regards
Sudhakar
0
 

Author Comment

by:cartman2000
ID: 11720289
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
ID: 11720301
oh sorry
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11720327
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
ID: 11720390
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
ID: 11720415
if you think the cookie is insecured store that session info at database
0
 

Author Comment

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

Expert Comment

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

Author Comment

by:cartman2000
ID: 11720839
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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11720862
create persistant cookies and try
0
 

Author Comment

by:cartman2000
ID: 11721763
any idea how I do that??
0
 
LVL 92

Assisted Solution

by:objects
objects earned 50 total points
ID: 11721924
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
ID: 11722169
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
ID: 11722218
all browser windows from the same process will share the same session (at least in IE)
0
 

Author Comment

by:cartman2000
ID: 11722323
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
ID: 11722347
> 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
ID: 11724543

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
ID: 11919601
thanks
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
spring jars download 1 35
runtime exception 2 41
Delphi Firemonkey: if the Sms contain special characters it won't send it 3 43
Eclipse for Java EE development 2 26
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

792 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