Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Shopping cart session is not working properly  RSS feed

 
Shahid Kahn
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
The cart is declared as instance variable like this
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Posts: 2982
216
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Tim Cooke
Marshal
Posts: 3870
233
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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?
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thank you all.
I got it solved by moving the variable into doPost method.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!