购物车
//首页显示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());
}
以上为代码简化版