File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Shopping cart  using session Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Shopping cart  using session" Watch "Shopping cart  using session" New topic

Shopping cart using session

Ranga Reddy shreya
Ranch Hand

Joined: Oct 20, 2004
Posts: 30
Hi There,
i am thinking to add items to shopping cart class. in jsp page i will take quantity and product sku .If the user select add cart i will take sku and quantity as a parametes in my servlet .my doubt is if he comes again and add more items then i have to change the quantity . i am keeping it in hash tabel.
Thanks in advance

Enjoy life .
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
What's your question?

Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Ranga Reddy shreya:
my doubt is if he comes again and add more items then i have to change the quantity

So, just change the quantity.
ernest fakudze
Ranch Hand

Joined: Aug 27, 2001
Posts: 216
Hi Ranga,

Not sure what your worry is but, here is how online shopping carts are typically implemented in JSP. First create a class to encapsulate a ShoppingCart item which maps to the product table in the database. Then when the user buys the first item (action=add), first check if you have a vector of ShoppingCartItem(s) in the session and if not, create it, add it to the session and and call it's addItem() method which should take the itemID as a parameter, no need to pass it any other info.

When the user updates the quantity for item X, iterate through the Vector and find its id, then call the setQuantity() method, which should take two parameters (itemID, newQuantity). If the user tries to add an item that's already in the shopping cart, just increment it's quantity by one, or ignore that command.

If you need some demonstration code, let me know and i will write you some quickly. Hope that helps.

In a time of drastic change it is the learners who inherit the future. The learned usually find themselves equipped to live in a world that no longer exists.<br />Eric Hoffer
I agree. Here's the link:
subject: Shopping cart using session
It's not a secret anymore!