欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jsp+servlet实现商城购物车功能

程序员文章站 2024-03-20 14:55:28
...
<a href="AddCart?id=${b.id}"><button class="btn btn-default">加入购物车<span class="glyphicon glyphicon-shopping-cart"></span></button></a>

商品详情页选中“加入购物车”后跳到AddCart

public class AddCart extends HttpServlet {
	
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id=request.getParameter("id");
		ProductService ps=new ProductService();
		Book b=ps.findBookById(id);
		HttpSession session=request.getSession();
		//从session中获取购物车对象
		Map<Book,Integer> cart=(Map<Book,Integer>)session.getAttribute("cart");
		//如果购物车为空,说明没有商品存储在购物车中,创建除购物车
		if(cart==null) {
			cart=new HashMap<Book,Integer>() ;	
		}
		//向购物车添加商品数量信息
		Integer count=cart.put(b, 1);
		//如果商品数量不为空,则商品数量+1,否则添加新的商品信息
		if(count!=null) {
			cart.put(b, count+1);
		}  
		session.setAttribute("cart", cart);
		request.getRequestDispatcher("cart.jsp").forward(request, response);;
	
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}

以map集合存储Book对象还有购买数量,并且新建购物车,存进session中

 <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}/ChangeNumServlet?id="+id+"&num="+num;
		}
	    </script>

 

<table cellpadding="1" style="width: 80%; height: auto;border:1px #8B0000 solid;text-align:center;margin-left:150px;">
			<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="total" value="0"/>
		 <c:forEach items="${cart}" var="entry" varStatus="vs">
		 <table width="80%" border="0" cellspacing="0" style="margin-left:150px;text-align:center">
		   <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.key.price*entry.value}</td>
		       <td width="10%">
		          <!-- 删除商品 -->
		          、            <a href="ChangeNumServlet?id=${entry.key.id}&num=0" style="color: #FF0000; font-weight: bold">X</a>
               </td>
		    </tr>
		 </table>
		 <c:set value="${total+ entry.key.price*entry.value}" var="total"/>
		 </c:forEach>
		 <br>
		<!-- 合计信息 -->
		<table style="margin-left:1000px">
		    <tr>
	            <td><font style="color: #FF6600; font-weight: bold">合计:  ${total}元</font>
	            </td>
	        </tr>
		</table>
		<br>
		<br>
		<br>
		<div style="padding-left:690px;">
           <a href="index.jsp"><button class="btn btn-info">继续购物</button></a>
           <a href="order.jsp"><button class="btn btn-default">立即买单</button></a>
           </div>	
public class ChangeNumServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id = request.getParameter("id");
		int num = Integer.parseInt(request.getParameter("num"));
		
		HttpSession session = request.getSession();
		Map<Book, Integer> cart = (Map<Book, Integer>) session.getAttribute("cart");
		ProductService ps=new ProductService();
		Book b=ps.findBookById(id);
		//如果商品数据为0,就删除对象
		if(num==0) {
			cart.clear();
		}else {
			cart.clear();
			cart.put(b, num);
		}
       
		request.getRequestDispatcher("cart.jsp").forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}

通过id找到对应的book对象后,从session中取出集合,要先清空集合再重新存键值(书和购买数量进去),不清空就会导致一会的cart页面出现多行从购物车里遍历取出来的数据