Session实现购物车
当对一件商品点击的添加购物车的操作
demo: 将点击的商品信息传入到后台做添加到session中的操作 |
public class AddCartServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter();
String id = request.getParameter("id");
BookServiceImpl bs = new BookServiceImpl();
Book b = bs.findBookById(id);
//从session中的购物车取出 来 HttpSession session = request.getSession(); Map<Book, String> cart = (Map<Book, String>) session.getAttribute("cart"); int num = 1; //如何是第一次访问,没有购物车对象,我们就创建 一个购物车对象 if(cart==null){ cart = new HashMap<Book, String>();
} //查看当前集合中是否存在b这本书,如果有就把数据取出来加1; if(cart.containsKey(b)){ num=Integer.parseInt(cart.get(b))+1; } //把图书放入购物车 cart.put(b, num+"");
//把cart对象放回到session作用域中 session.setAttribute("cart", cart);
out.print("<a href='"+request.getContextPath()+"/servlet/pageServlet'>继续购物</a>,<a href='"+request.getContextPath()+"/cart.jsp'>查看购物车</a>"); }
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }
} |
demo: 修改购物车中信息jsp代码 |
<script type="text/javascript"> function changeNum(id,num,totalCount){ num = parseInt(num); totalCount =parseInt(totalCount);
if(num<1){ if(confirm("是否确认要删除此商品?")){ num = 0; }else{ num=1; } }
if(num>totalCount){ alert("购买数量不能大于库存数量!"); num=totalCount; } location.href="${pageContext.request.contextPath}/servlet/changeNumServlet?id="+id+"&num="+num; } </script>
</head>
<body class="main">
<jsp:include page="head.jsp" />
<jsp:include page="menu_search.jsp" />
<div id="divpagecontent"> <table width="100%" border="0" cellspacing="0"> <tr>
<td><div style="text-align:right; margin:5px 10px 5px 0px"> <a href="index.html">首页</a> > 购物车 </div>
<table cellspacing="0" class="infocontent"> <tr> <td><img src="ad/page_ad.jpg" width="666" height="89" /> <table width="100%" border="0" cellspacing="0"> <tr> <td><img src="images/buy1.gif" width="635" height="38" /> </td> </tr> <tr> <td> <table cellspacing="1" class="carttable"> <tr> <td width="10%">序号</td> <td width="30%">商品名称</td> <td width="10%">价格</td> <td width="20%"> 数量</td> <td width="10%">库存</td> <td width="10%">小计</td> <td width="10%">取消</td> </tr> </table> <c:set var="sum" value="0" > </c:set> <c:forEach items="${cart }" var="entry" varStatus="vs"> <table width="100%" border="0" cellspacing="0"> <tr> <td width="10%">${vs.count }</td> <td width="30%">${entry.key.name }</td>
<td width="10%">${entry.key.price }</td> <td width="20%"><input type="button" value='-' style="width:20px" onclick="changeNum('${entry.key.id}','${entry.value-1 }','${entry.key.pnum }')">
<input name="text" type="text" value="${entry.value }" style="width:40px;text-align:center" /> <input type="button" value='+' style="width:20px" onclick="changeNum('${entry.key.id}','${entry.value+1 }','${entry.key.pnum }')">
</td> <td width="10%">${entry.key.pnum }</td> <td width="10%">${entry.value*entry.key.price }</td>
<td width="10%"><a href="${pageContext.request.contextPath}/servlet/changeNumServlet?id=${entry.key.id}&num=0" style="color:#FF0000; font-weight:bold">X</a></td> </tr> </table> <c:set var="sum" value="${sum+entry.value*entry.key.price }"> </c:set> </c:forEach>
<table cellspacing="1" class="carttable"> <tr> <td style="text-align:right; padding-right:40px;"><font style="color:#FF6600; font-weight:bold">合计: ${sum}元</font> </td> </tr> </table> <div style="text-align:right; margin-top:10px"> <a href="${pageContext.request.contextPath}/showProductByPage"><img src="images/gwc_jx.gif" border="0" /> </a>
<a href="${pageContext.request.contextPath}/order.jsp"><img src="images/gwc_buy.gif" border="0" /> </a> </div> </td> </tr> </table> </td> </tr> </table></td> </tr> </table> </div>
<jsp:include page="foot.jsp" />
</body> </html> |
后台处理代码 |
public class ChangeNumServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id = request.getParameter("id"); String num = request.getParameter("num"); //注:只能重写id的hashcode Book b = new Book(); b.setId(id);
HttpSession session = request.getSession(); Map<Book, String> cart = (Map<Book, String>) session.getAttribute("cart"); //如果商品数据为0,就删除对象 if("0".equals(num)){ cart.remove(b); } //判断如果找到与id相同的书, if(cart.containsKey(b)){ cart.put(b, num); }
response.sendRedirect(request.getContextPath()+"/cart.jsp"); }
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }
} |