java简易购物车源代码 java做购物车( 六 )


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 购物车处理类
* @author @author ITJob 远标培训
*
*/
import com.kaka.entity.Items;
import com.kaka.entity.Product;
import com.kaka.service.ProductService;
import com.kaka.service.impl.ProductServiceImpl;
public class HomeCar extends HttpServlet {
private static final long serialVersionUID = 1L;
ProductService ps = new ProductServiceImpl();
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取商品的id
String proId = req.getParameter("proId");
resp.setContentType("text/html;charset=UTF-8");
PrintWriter writer = resp.getWriter();
if(null != proId!"".equals(proId)){
//返回添加购物车成功
//System.out.println("=============" + proId);
//根据商品的id查询商品
try {
Integer pId = Integer.parseInt(proId);
Product product = ps.findProductById(pId);
if(null != product){
//查询到java简易购物车源代码了商品,将商品的相关参数构建一个购物明细放入到购物车
Items it = new Items();
it.setProId(product.getProId());
it.setProName(product.getProName());
it.setProPrice(product.getProPrice());
it.setProImg(product.getProImg());
//先判断session范围是否有购物车
ListItems shopCar = (ListItems)req.getSession().getAttribute("shopCar");
if(null == shopCar){
//购物车
shopCar = new ArrayListItems();
}
//将商品加入到购物车之前 , 判断购物车中是否已经包含了该购物明细,如果包含了 , 只需要修改购买的数量
if(shopCar.contains(it)){
int index= shopCar.indexOf(it);//寻找购物车中包含购物明细在购物车中位置
Items items = shopCar.get(index);//获取购物车中存在的购物明细
items.setProNum(items.getProNum()+1);
} else {
shopCar.add(it);
}
//将购物车放入到session访问
req.getSession().setAttribute("shopCar", shopCar);
//返回
writer.print(true);
} else {
writer.print(false);
}
} catch (Exception e) {
e.printStackTrace();
writer.print(false);
}
} else {
writer.print(false);
}
writer.flush();
writer.close();
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
后台管理servlet
package com.kaka.web;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.mail.FetchProfile.Item;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 购物车修改
* @author ITJob 远标培训
*
*/
import com.kaka.entity.Items;
import com.kaka.entity.Product;
import com.kaka.service.ProductService;
import com.kaka.service.impl.ProductServiceImpl;
public class HomeCarManager extends HttpServlet {
private static final long serialVersionUID = 1L;
ProductService ps = new ProductServiceImpl();
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=UTF-8");
PrintWriter writer = resp.getWriter();
//获取参数
String proId = req.getParameter("proId");
String num = req.getParameter("num");
if(null != proIdnull != num
!"".equals(proId)!"".equals(num)){

推荐阅读