how to make shopping cart safe from refresh button


Web tier: servlets, JSP, Web frameworks: how to make shopping cart safe from refresh button

  1. We are dealing with an on-line shopping project.
    When customer click refresh button on browser, the previous action will be repeated again, eg. adding the same product as he added before again or removing another product.

    What can we do to the refresh button so that it will not repeated the previous action when customer click it?

    Thanks !
  2. If your using a Map such as a Hashtable (as your cart) you should be ok. The product being added(again) will just replace itself. The remove should be fine too.

    Hope that helps,

  3. Unfortunately,[ Go to top ]

    I use ArrayList. It seems that using HashMap is much more complicated in coding than using ArrayList. Thus I chose the latter when we did design. Is there any other way that can prevent the problem from happening without changing the data structure?

    Thanks, Wes!
  4. Unfortunately,[ Go to top ]

    A Map allows you to "map" a key to a value, which suits a shopping cart much better. Your key can be a productid, and the value is the entire product. If you have products with multiple colors for example, your key could be comination of the productid and the color. Ive done something similar and it worked out nicely.

    The Hashtable which implements Map is not complicated at all.

    Here is a quick snippet...

    class Cart {
      Hashtable contents = new Hashtable();

      public void addItem(Item i) {
         contents.put(, item);
      public void removeItem(String itemid) {

  5. Unfortunately,[ Go to top ]

    For the ArrayList, I guess you could just check for the existence of the object your about to add...

    if(!arrayList.contains(product)) {
      //add it

    if(arrayList.contains(product)) {
      //remove it
  6. Thank you. Weston[ Go to top ]

    Thank you. Weston

  7. The easiest way to do this is via a Synchronizer Token Pattern. You can find out more about it in Core J2EE patterns (page 43) and on sun's site. Struts also has a good implementation of Token usage for form submission. Token mismatch is a pretty good way to do what you want. It's fairly common, and more robust.
  8. Thank you. I will check J2EE pattern.
  9. Most of the suggestions so far have had to do with the data structure that you might use or are using. Let me suggest a different route.

    I'm not sure what framework you are using, or if you are using one at all. What I would do is to seperate out the "showing" of the shopping cart from the add. Then, add the item and forward/redirect to the "showCart" action or page. By doing this, the last action becomes the "showCart" action and if the user presses refresh, it just shows the cart again.

    Counting on the data structure being one type or another might work, but that's not the real problem. The real problem is that the last action was addItem. Refresh is doing what it should. Just change how you show the cart after the addItem is done by calling a seperate action to show the cart.

    Just my preference,
  10. we use different approach. we assosiate sequenceId(number) with each shopping cart "action" (add/remove/update). Sometimes it accessable via get parameter, sometimes via browser "session" cookie. You may find implementation at (This is an example application we use to show to the customers, we do outsourcing). BTW shopping cart itself stored or in cookie, or in session which of them are supported by browser or its configuration.
  11. shop