购物车实现

购物车

//首页显示servlet

//获取所有商品信息

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

.//打印在浏览器上

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

For(Goodsentity:list){

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

}

//ServletBy,do

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

Stringrid=request.getParameter("id");

//类型转换

Integerid=Integer.parseInt(rid);

//执行查询

Goodsentity=gService.findById(id);

//session中获取carts

HttpSessionsession=request.getSession();

Cookiecookie=newCookie("JSSSIONID",session.getId());

cookie.setMaxAge(600);

cookie.setPath("/20111025");

response.addCookie(cookie);

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

If(carts==null){

Carts=newArrayList<Goods>();

session.setAtrribute("carts",carts);

}

Carts.add(entity);

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

response.sendRedirect(url);

//list.do

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

For(Goodsentity:carts){

Out.println(entity.getName());

}

以上为代码简化版

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值