购物车实现
AddCartServlet.java
package com.exp.web.servlet; import com.exp.bookstore.model.Product; import com.exp.bookstore.service.ProductService; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.sql.SQLException; import java.util.HashMap; import java.util.Map; @WebServlet("/addCart") public class AddCartServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.获取id String id = request.getParameter("id"); //2.通过id查询数据库对应商品 ProductService ps = new ProductService(); Product p = ps.findBook(id); //3.把购买商品放在购物车Map //3.1先从session获取购物车数据[cart] Map<Product,Integer> cart = (Map<Product, Integer>) request.getSession().getAttribute("cart"); //3.2如果没有购物车数据,就创建一个map对象 if(cart == null){ cart = new HashMap<Product,Integer>(); cart.put(p,1);//首次加入购物车 }else{ //3.3 判断map里面是否有当前想购物商品 if(cart.containsKey(p)){ cart.put(p,cart.get(p)+1); }else{ cart.put(p,1); } } //4.打印购物车数据 for(Map.Entry<Product,Integer> entry: cart.entrySet()){ System.out.println(entry.getKey() + "数量" + entry.getValue()); } //5.存session request.getSession().setAttribute("cart",cart); //6.响应客户端面 //继续购物,查看购物车 //6.响应客户端面 //继续购物,查看购物车 String a1 = "<a href=\"" + request.getContextPath()+"/showProductByPage\">继续购物</a>"; String a2 = " <a href=\"" + request.getContextPath()+"/cart.jsp\">查看购物车</a>"; response.getWriter().write(a1);//write可以多次写入 response.getWriter().write(a2); } public static void main(String[] args) { //map Map<Product, Integer> cart = new HashMap<Product, Integer>(); ProductService ps = new ProductService(); Product p1 = ps.findBook("2"); cart.put(p1, 1); Product p2 = ps.findBook("2"); /** * 默认情况下,key是根据地址来判断是否存在 * 所以要改比较规则,重写equals方法 */ if(cart.containsKey(p2)){ System.out.println("p2已经在购物车存在"); }else{ System.out.println("p2在购物车不存在"); } } }
cart.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>电子书城</title> <link rel="stylesheet" href="css/main.css" type="text/css" /> </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> <table width="100%" border="0" cellspacing="0"> <%--定义一个总价格的变量--%> <c:set var="totalPrice" value="0"></c:set> <c:forEach items="${cart}" var="entry"> <tr> <td width="10%">1</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"> <input name="text" type="text" value="${entry.value}" style="width:40px;text-align:center" /> <input type="button" value='+' style="width:20px"> </td> <td width="10%">${entry.key.pnum}</td> <td width="10%">${entry.value*entry.key.price}</td> <td width="10%"><a href="#" style="color:#FF0000; font-weight:bold">X</a></td> </tr> <!-- 累计总价格 --> <c:set var="totalPrice" value="${totalPrice+ entry.value*entry.key.price}"></c:set> </c:forEach> </table> <table cellspacing="1" class="carttable"> <tr> <td style="text-align:right; padding-right:40px;"><font style="color:#FF6600; font-weight:bold">合计: ${totalPrice}元</font> </td> </tr> </table> <div style="text-align:right; margin-top:10px"> <a href="product_list.jsp"><img src="images/gwc_jx.gif" border="0" /> </a> <a href="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>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?