![]() |
#2
猪猪002016-04-16 18:03
|
只有本站会员才能查看附件,请 登录
只有本站会员才能查看附件,请 登录

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
if(request.getParameter("action")!=null){
this.action = request.getParameter("action");
if(action.equals("add"))//如果是添加商品进购物车
{
if(addToCart(request,response)){
request.getRequestDispatcher("/success.jsp").forward(request, response);
}else{
request.getRequestDispatcher("/failure.jsp").forward(request, response);
}
}
if(action.equals("show"))//如果是显示购物车商品
{
// showCart(request,response);
}
}
}
//添加商品到购物车
private boolean addToCart(HttpServletRequest request, HttpServletResponse response)
{
String id = request.getParameter("id");
String number = request.getParameter("num");
Items item = idao.getItemsById(Integer.parseInt(id));
//如果是第一次添加商品到购物车,就要在session中创建一个购物车类
if(request.getSession().getAttribute("cart")==null){
Cart cart = new Cart();
request.getSession().setAttribute("cart", cart);
}
Cart cart = (Cart)request.getSession().getAttribute("cart");
if(cart.addItems(item, Integer.parseInt(number.trim()))){
return true;
}
else{
return false;
}
}
[此贴子已经被作者于2016-4-16 15:20编辑过]