• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with session  RSS feed

 
Amir Ghahrai
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am developing a simple shopping cart application, using JSP and servlets.
my problem is that whenever, I add an item to the cart, it overrides the previous ones.
For instance, when I view the cart with 1 item in it it will show,
number of items 1, item1
when I add a second item it will show
number of items 2, item2, item2.
instead of printinh
number of items 2, item1, item2.
I cannot see where this problem is coming from.
any suggestions?
Thanks
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Usually, a shopping cart uses a session that holds a collection type object which in turn holds an instance of some object that represents a purchased item.
How are you representing a purchased item?
What kind of collection are you using to hold purchased items?
Bill
 
Amir Ghahrai
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill,
Basically, I'm using a Vector to store the items.
I have thought about using a Hashmap instead, but the book that I'm following the example from, uses a Vector to store the shopping cart items in it.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you representing a single item?
Your symptoms sort of remind me of a case in which only one instance of a purchased item existed and it got added to the Vector multiple times.
Bill
 
Amir Ghahrai
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill, thanks for your reply. This is exactly what is happening, but I cannot see where in my code I am going wrong.
I will describe a little bit more about the application.
I have an Item class which has a list of get/set methods that represent the properties of an item, such as name, description, price as well as a unique ID.
I also have a Catalog class which retrieves the Item data from the database and stores them in an array.
This class has two methods, one which returns an array of product ID's, and another method which instantiates an Item object for a given product ID,

in my Shopping cart class I have an addItem method which takes the productID as a String parameter and constructs an Item object and adds it to the Vector.
(I am guessing that this is where the problem occurs, that it overwrites the previous Item objects)

Hope this will clarify my problem.
Thanks for your help.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't see any obvious problem.
I think I would provide some way to dump the current contents of that vector to a log file every time it is modified - maybe that will show something up.
I do hope the code

was abbreviated to keep your post short and you really are not throwing away exceptions.
Bill
 
Amir Ghahrai
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bill,
After much scrutiny of my code, I finally could see where the problem is coming from. I have a method similar to the method below

suppose I have 5 items.
Inside the for loop, the code that prints out the name of each item seems to be fine, but outside the for loop, it will print only the last element
5 times. In other words, the vector seems to "reset" each time and the value I get returned is a vector with the last value stored from the for loop, and not a vector of different values.
sample output
 
Amir Ghahrai
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a problem with the session then, as the subject of the post indicate!
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That illustrates an important point (IMHO) about developing for servlets and JSP - you should make as much as possible of your application testable off-line - there are so many things that can go wrong in the servlet environment that you frequently can't even roughly tell where things went wrong.
Bill
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!