title: how to print values on a JSP page from a hashtable using useBean
body: <p>I have created a shopping cart application using jsp and servlets.</p>
  
  <p>I am able to add items to shopping cart.</p>
  
  <p>But I want to show the items on <strong>"View Shopping Cart"</strong> jsp page  in <strong>following table format.</strong></p>
  
  <pre><code>Item    |   Quantity   |   Price
  book1   |       1      |   100
  book2   |       2      |   200
  Total   |       3      |   300
  </code></pre>
  
  <p>Also I want <code>updateQuantity()</code> and <code>removeFromCart()</code> functions in <code>ViewCart.jsp</code> page.</p>
  
  <p>How should I do it?</p>
  
  <p>The code for the pages is as follows.</p>
  
  <hr>
  
  <p><strong>ShoppingCart.java</strong></p>
  
  <pre><code> package beans;
  
  import java.util.Enumeration;
  import java.util.Hashtable;
  
  public class ShoppingCart {
  
  protected Hashtable items=new Hashtable();
  
  
  
  public void addItem(String itemId,String desc,float price,int quantity)
  {
      String [] item={itemId,desc,Float.toString(price),Integer.toString(quantity)};
  
      if(items.containsKey(itemId))
      {
          String [] tmpItem=(String[])items.get(itemId);
          int tmpQuant=Integer.parseInt(tmpItem[3]);
          quantity+=tmpQuant;
          tmpItem[3]=Integer.toString(quantity);
      }
  
      else
      {
          items.put(itemId,item);
      }
  }
  
  public void removeItem(String itemId)
  {
      if(items.containsKey(itemId))
      {
          items.remove(itemId);
      }
  }
  
  public void updateQuantity(String itemId,int quantity)
  {
      if(items.contains(itemId))
      {
          String [] tmpItem=(String[])items.get(itemId);
          tmpItem[3]=Integer.toString(quantity);
      }
  }
  
  public Enumeration getEnumeration()
  {
      return items.elements();
  }
  
  public float getCost()
  {
      Enumeration enumeration=items.elements();
      String [] tmpItem;
      float totalCost=0.00f;
  
      while(enumeration.hasMoreElements())
      {
          tmpItem=(String[]) enumeration.nextElement();
          totalCost+=(Integer.parseInt(tmpItem[3]))*(Float.parseFloat(tmpItem[2]));
      }
  
      return totalCost;
  }
  
  public int getNumberOfItems()
  {
      Enumeration enumeration=items.elements();
      String [] tmpItem;
      int numberOfItems=0;
  
      while(enumeration.hasMoreElements())
      {
          tmpItem=(String[])enumeration.nextElement();
          numberOfItems+=Integer.parseInt(tmpItem[3]);
      }
  
      return numberOfItems;
  }
  
  public String[] showCart()
  {
      String itemId = null;
      String [] tmpItem = null;
      int tmpId;
      String desc = null;
      float price = 0;
      int quantity = 0;
      String [] item={itemId,desc,Float.toString(price),Integer.toString(quantity)};
      Enumeration enumeration=items.elements();
      while(enumeration.hasMoreElements())
      {
           tmpItem=(String[])enumeration.nextElement();
          tmpId=Integer.parseInt(tmpItem[0]);
  
  
      }
  
  
     return tmpItem;
  }
  }
  </code></pre>
  
  <hr>
  
  <p><strong>Product.jsp</strong></p>
  
  <pre><code>&lt;%@page import="beans.ShoppingCart"%&gt;
  &lt;%@page import="java.util.*" %&gt;
  &lt;%@page import="javax.servlet.http.*" %&gt;
  &lt;jsp:useBean id="cart" scope="session" class="beans.ShoppingCart" /&gt;
  
  &lt;head&gt;
  &lt;title&gt;Product&lt;/title&gt;  
  &lt;/head&gt;  
  
  &lt;body&gt;     
     &lt;p&gt;Shopping Cart  Quantity : &lt;%=cart.getNumberOfItems()  Price : &lt;%=cart.getCost() %&gt;&lt;/p&gt;
     &lt;h2&gt;Book 1 &lt;/h2&gt;&lt;br&gt;
  
     &lt;h2&gt; Price :  250&lt;/h2&gt;&lt;br&gt;
  
     &lt;form action="Product.jsp" method="POST"&gt;
      &lt;input type="submit" name="Submit" value="Add to Cart"&gt;
      &lt;input type="hidden" name="id" value="1"&gt;
      &lt;input type="hidden" name="desc" value="Book 1"&gt;
      &lt;input type="hidden" name="price" value="250"&gt;
    &lt;/form&gt;
  
    &lt;h2&gt; BOOK 2&lt;/h2&gt;&lt;br&gt;
  
    &lt;h2&gt; Price : 300&lt;/h2&gt;&gt;&lt;br&gt;    
  
    &lt;form action="Product.jsp" method="POST"&gt;
    &lt;input type="submit" name="Submit" value="Add to Cart"&gt;
      &lt;input type="hidden" name="id" value="2"&gt;
      &lt;input type="hidden" name="desc" value="Book 2"&gt;
      &lt;input type="hidden" name="price" value="300"&gt;
    &lt;/form&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>