package com.fendou.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.fendou.db.GoodsDB;
import com.fendou.vo.CarManager;
import com.fendou.vo.Goods;
import com.fendou.vo.Item;
public class UpdateGoodsCarServlet extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @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 doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.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 {
request.setCharacterEncoding("utf-8");
//得到商品的ID
int pid = Integer.parseInt(request.getParameter("gid"));
//得到数量
int count = 1;
try{
count = Integer.parseInt(request.getParameter("count"));
}catch(Exception e){}
//到数据库查找对应的商品信息
Goods g = GoodsDB.findById(pid);
//创建session
//从session中取出购物车
HttpSession session = request.getSession();
List<Item> itemList = (List<Item>)session.getAttribute("iList");
CarManager cm = new CarManager();
cm.setItemList(itemList);
cm.updateCar(g, count);
//得到最近的List
itemList = cm.getItemList();
//System.out.println("itemList:" + itemList);
double totalPrice = 0;
//计算总价格
for (Item item : itemList) {
totalPrice += item.getCount()*item.getGood().getPrice();
}
session.setAttribute("iList", itemList);
session.setAttribute("totalPrice", totalPrice);
request.getRequestDispatcher("car.jsp").forward(request, response);
}
}
评论21
最新资源