请热心的高手帮小弟看看,(100分)

  • 主题发起人 主题发起人 leon2008
  • 开始时间 开始时间
L

leon2008

Unregistered / Unconfirmed
GUEST, unregistred user!
我做了一个购物车程序
做了一个类用来存储购物车内的产品信息如下:
public class ProductInCart{
private String productID;
//产品编号
private int productQuantity;
//产品数量
private float productPrice;
//产品单价
public String getProductID(){
return productID;
}
public void setProductID(String value){
productID = value;
}
public int getProductQuantity(){
return productQuantity;
}
public void setProductQuantity(int value){
productQuantity = value;
}
public float getProductPrice(){
return productPrice;
}
public void setProductPrice(float value){
productPrice = value;
}
public ProductInCart(){
}
}

然后用Hashtable声明一个购物车类用来取出购物车中的产品
Hashtable myCart = (Hashtable)session.getValue("MyCart");
Enumeration enum = myCart.keys();
while (enum.hasMoreElements()){
//一执行下面这条语句就出错,说是servlet错误
ProductInCart productInfo = (ProductInCart)enum.nextElement();
//
out.print(productInfo.getProductID());
out.print(productInfo.getProductQuantity());
out.print(productInfo.getProductPrice());
out.println("-----");
}
}else
{
out.println("购物车为空");
}
//加入商品的代码如下
//取得当前要加入的商品的信息,将信息存入购物车类中
ProductInCart productInfo = new ProductInCart();
productInfo.setProductID(request.getParameter("productID"));
productInfo.setProductQuantity(Integer.parseInt(request.getParameter("productQuantity")));
productInfo.setProductPrice(Float.parseFloat(request.getParameter("productPrice")));


Hashtable myCart = (Hashtable)session.getValue("MyCart");
if (myCart == null) {
myCart = new Hashtable();
}
//检查当前加入的产品的编号是否已存在购物车中,
//如果存在则增加其数量,如果没有则加入该产品
String productID = productInfo.getProductID();
if (myCart.containsKey(productID) == true){
int productQuantity = productInfo.getProductQuantity() +
((ProductInCart)myCart.get(productID)).getProductQuantity();
productInfo.setProductQuantity(productQuantity);
}
myCart.put(productInfo.getProductID(), productInfo);
//将购物车对象存入session中
session.putValue("MyCart", myCart);
out.println("您的商品已加入购物车中");
错误信息如下
Error: 500
Location: /lw/CartView.jsp
Internal Servlet Error:
javax.servlet.ServletException
at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:449)
at lw._0002flw_0002fCartView_0002ejspCartView_jsp_14._jspService(_0002flw_0002fCartView_0002ejspCartView_jsp_14.java:100)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:119)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:177)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:309)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:382)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:387)
at org.apache.tomcat.core.Handler.service(Handler.java:263)
at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:371)
at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:786)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:732)
at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:210)
at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:407)
at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:498)
at java.lang.Thread.run(Thread.java:536)
Root cause:
java.lang.ClassCastException
at lw._0002flw_0002fCartView_0002ejspCartView_jsp_14._jspService(_0002flw_0002fCartView_0002ejspCartView_jsp_14.java:80)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:119)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:177)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:309)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:382)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:387)
at org.apache.tomcat.core.Handler.service(Handler.java:263)
at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:371)
at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:786)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:732)
at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:210)
at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:407)
at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:498)
at java.lang.Thread.run(Thread.java:536)
 
大侠们,小弟已经解决了,原因出在这句上
Enumeration enum = myCart.keys();
将其改为
Enumeration enum = myCart.elements();
就好了
 
好,一百分我也有份了
 
后退
顶部