Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用useBean从哈希表打印JSP页面上的值_Jsp_Shopping Cart - Fatal编程技术网

如何使用useBean从哈希表打印JSP页面上的值

如何使用useBean从哈希表打印JSP页面上的值,jsp,shopping-cart,Jsp,Shopping Cart,我已经使用jsp和servlet创建了一个购物车应用程序 我可以添加项目到购物车 但是我想用下表的格式显示“查看购物车”jsp页面上的项目。 Item | Quantity | Price book1 | 1 | 100 book2 | 2 | 200 Total | 3 | 300 我还需要updateQuantity()和removeFromCart()函数在ViewCart.jsp

我已经使用jsp和servlet创建了一个购物车应用程序

我可以添加项目到购物车

但是我想用下表的格式显示“查看购物车”jsp页面上的项目。

Item    |   Quantity   |   Price
book1   |       1      |   100
book2   |       2      |   200
Total   |       3      |   300
我还需要
updateQuantity()
removeFromCart()
函数在
ViewCart.jsp
页面中

我该怎么做

页面的代码如下所示


ShoppingCart.java

 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;
}
}

Product.jsp

<%@page import="beans.ShoppingCart"%>
<%@page import="java.util.*" %>
<%@page import="javax.servlet.http.*" %>
<jsp:useBean id="cart" scope="session" class="beans.ShoppingCart" />

<head>
<title>Product</title>  
</head>  

<body>     
   <p>Shopping Cart  Quantity : <%=cart.getNumberOfItems()  Price : <%=cart.getCost() %></p>
   <h2>Book 1 </h2><br>

   <h2> Price :  250</h2><br>

   <form action="Product.jsp" method="POST">
    <input type="submit" name="Submit" value="Add to Cart">
    <input type="hidden" name="id" value="1">
    <input type="hidden" name="desc" value="Book 1">
    <input type="hidden" name="price" value="250">
  </form>

  <h2> BOOK 2</h2><br>

  <h2> Price : 300</h2>><br>    

  <form action="Product.jsp" method="POST">
  <input type="submit" name="Submit" value="Add to Cart">
    <input type="hidden" name="id" value="2">
    <input type="hidden" name="desc" value="Book 2">
    <input type="hidden" name="price" value="300">
  </form>

</body>
</html>

产品
购物车数量:

现在是学习Javabeans和JSTL的时候了。另请参见,您好,请告诉我如何以上述表格格式打印哈希表中的值。