• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Shopping cart session is not working properly

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am adding my shopping cart in session like bellow in servlet

I seems if more than one person accesses it from server, if they items in the shopping cart, all the product is added to a single session. I mean if i added 4 items first and other person adds 2 items, second person sees 6 products in his cart. Where am I going wrong?
 
Shahid Kahn
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The cart is declared as instance variable like this
 
Creator of Enthuware JWS+ V6
Posts: 3411
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is that there is only one instance of a Servlet. When you declare your cart as an instance variable it will be shared.
 
Shahid Kahn
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please suggest me the solution?

Should I crate a ShoppingCart class with an instance variable of then use a ShoppingCart local variable in the servlet?
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I assume this is a follow on from your previous thread?

The answer is to not declare the 'cart' as an instance member because, as Frits has said, there is only one instance that is shared be everybody. In your previous thread you were doing it right, why the change?
 
Shahid Kahn
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Tim. It is a follow on from previous thread. I am going to change the code again. Actually I am so confused because I understand the concepts as pieces but when I try to integrate all the concepts to develop an application I face lot of difficulties. Could you please suggest me any reading material on how to combining all the concepts to develop an application?
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess any book gives out pieces which we are supposed to put together. That is the aim of learning IMO.
No one can construct a building or make a car holding a book in their hand.

Now back to your problem in this thread.

Shahid Kahn wrote:I mean if i added 4 items first and other person adds 2 items, second person sees 6 products in his cart. Where am I going wrong?


Here, the "two different" people are from your perspective. The application is not seeing them as different. Which means your session is either not created or destroyed properly.
Mostly I would say the creation part. Are you seeing the same behavior if you try two different browsers?
 
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shahid Kahn wrote:The cart is declared as instance variable like this


Servlets should never have instance variables because they are not thread safe as you've discovered (well, maybe if they are static and final, but even that is debatable). Put your variables in the doPost(or doGet) method. Each request to doPost() spawns a new thread and each thread will get it's own copy of the variables. Here is a good article on the topic.
 
Shahid Kahn
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all.
I got it solved by moving the variable into doPost method.
 
Does this tiny ad smell okay to you?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic