• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to remove item from shopping cart?  RSS feed

 
Karen Wilson
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created a website using jsp and servlet. I made a shopping cart and got the items to display. But how can i remove the items from the cart? Next to each item is a remove button. When that is pressed the item selected needs to be removed and cart needs to refresh.

Here is the code to display the cart:


and here is the servlet:
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the button is clicked the form will submit. You need to supply an action for the form that invokes a task servlet that controls the process of removing the item from the model. That task servlet can then redirect to the page controller for the cart page which should repaint with the new list.

(I'm assuming you're not wanting to get into Ajax and removing the element without refreshing the page.)
 
Karen Wilson
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thanks but what needs to go into the servlet to remove the item?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have no idea. Whatever is storing the list of cart items needs to be instructed to remove the item.

And actually, the servlet shouldn't be the component changing the model -- that should be in the model layer.

 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As an aside, the presence of a "processRequest" method leads me to believe that you might be treating GET and POST interchangeably, i.e. doGet and doPost both call processRequest. If that is indeed the case, it will be a problem, since treating GET and POST the same violates the HTTP specification, and will likely lead to problems.

What you currently have in processRequest looks like it should go into doGet, whereas code for removing items from the shopping cart would go into doPost.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!