一个购物车 表示看不懂哪错了 求大神指教
servlet吧
全部回复
仅看楼主
level 1
心念念_ 楼主
package servlets;
import beans.*;
import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ShoppingServlet extends HttpServlet{
public void doPost(HttpServletRequest req,HttpServletResponse res)
throws ServletException, IOException{
HttpSession session=req.getSession(false);
String action = req.getParameter("action");
if(action==null) action="";
RequestDispatcher view;
HashMap cart=(HashMap)session.getAttribute("shopping_cart");
if(action.equals("ADD")){
Product product = getProduct(req);
if(cart==null){
cart= new HashMap();
cart.put(product.getName(),product);
}
else{
Product oldP=null;
oldP=(Product)cart.get(product.getName());
if(oldP!=null){
product.setQuantity(oldP.getQuantity()+product.getQuantity());
}
cart.put(product.getName(),product);
}
session.setAttribute("shopping_cart",cart);
view=req.getRequestDispatcher("Cart.jsp");
res.setContentType("text/html;charset=GB2312");
view.forward(req,res);
}
else if(action.equals("DELETE")){
String quantityName=null;
quantityName=req.getParameter("quantityName");
if(quantityName!=null)
cart.remove(quantityName);
view=req.getRequestDispatcher("Cart.jsp");
res.setContentType("text/html;charset=GB2312");
view.forward(req,res);
}
}
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException, IOException{
doPost(req,res);
}
public Product getProduct(HttpServletRequest req) throws ServletException, IOException{
String info=req.getParameter("disk");
String quantityStr=req.getParameter("quantity");
StringTokenizer t=new StringTokenizer(info,"|");
String disk=t.nextToken();
String priceStr=t.nextToken();
int price=0,quantity=0;
try{
quantity=Integer.parseInt(quantityStr);
price=Integer.parseInt(priceStr);
}catch(NumberFormatException e){
e.printStackTrace();
}
Product product=new Product();
product.setName(disk);
product.setPrice(price);
product.setQuantity(quantity);
return product;
}
}
报错是 HTTP Status 500 -
type Exception report
message
description The server encountered an internal error that prevented it from fulfilling this request.
exception
java.util.NoSuchElementException java.util.StringTokenizer.nextToken(StringTokenizer.java:349)
servlets.ShoppingServlet.getProduct(ShoppingServlet.java:60)
servlets.ShoppingServlet.doPost(ShoppingServlet.java:19)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.37 logs.
2013年05月19日 14点05分 1
level 1
心念念_ 楼主
已经解决了 是传过来的表单的问题
2013年05月20日 16点05分 3
o.o
2013年07月09日 02点07分
level 5
o.0
2013年07月09日 02点07分 4
level 1
表示看不懂但是感觉很厉害的样子
2014年08月22日 03点08分 5
1