购物车实现

来源:互联网 发布:手机淘宝怎么退款申请 编辑:程序博客网 时间:2024/05/17 03:23

购物车

//首页显示servlet

//获取所有商品信息

List<Goods> list = gService.findAll();

.//打印在浏览器上

String url = response.encodeRedirectURL("/20111025/by.do");

For(Goods entity:list){

Out.println(entity.getName()+"<a href='"+url+"?id="+entity.getId()+"'>加入购物车 </a>");

}

//Servlet  By,do

//获取传递的参数id的值

String rid = request.getParameter("id");

//类型转换

Integer id = Integer.parseInt(rid);

//执行查询

Goods entity = gService.findById(id);

//session中获取carts

HttpSession session = request.getSession();

Cookie cookie = new Cookie("JSSSIONID",session.getId());

cookie.setMaxAge(600);

cookie.setPath("/20111025");

response.addCookie(cookie);

List<Goods> carts = (List<Goods>) session.getAttribute("carts");

If(carts==null){

Carts = new ArrayList<Goods>();

session.setAtrribute("carts",carts);

}

Carts.add(entity);

String url = response.encodeRedirectURL("/20111025/list.do");

response.sendRedirect(url);

//list.do

List<Goods> carts = request.getSession().getAttribute("carts");

For(Goods entity:carts){

Out.println(entity.getName());

}

以上为代码简化版

原创粉丝点击