📄 shopcarproc.java
字号:
package com.xaccp.servlet;
import java.io.IOException;
import java.io.PrintWriter;
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.xaccp.business.ProductManager;
import com.xaccp.business.ShopCar;
import com.xaccp.common.StrLib;
import com.xaccp.entity.Product;
public class ShopCarProc extends HttpServlet {
public ShopCarProc() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
ProductManager prodManager = new ProductManager();
String prodId = StrLib.toChinese(request.getParameter("prodId"),
"UTF-8");
HttpSession session = request.getSession();
Object obj = session.getAttribute("shopCar");
ShopCar shopCar = null;
if (obj == null) {
shopCar = new ShopCar();// 会话空间中无购物车对象,新创建
} else {
shopCar = (ShopCar) obj;
}
Product prod = shopCar.getProdFromCar(prodId);
if (prod == null)// 购物车无此货物,则从数据库中获取
{
prod = prodManager.getAProduct(Integer.parseInt(prodId));
prod.setProdCount(1);
} else {
prod.setProdCount(prod.getProdCount() + 1);
}
// 商品对象加之购物车
shopCar.addProduct(prodId, prod);
// 存储在当前会话
session.setAttribute("shopCar", shopCar);
response.sendRedirect("productCar.jsp");
out.flush();
out.close();
}
public void init() throws ServletException {
// Put your code here
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -