用jsp实现一个简单的购物车web应用系统。实现的添加购物商品,删除购物商品并且显示购物车信息。

article/2025/10/17 22:48:16

jsp实现一个简单的购物车web应用系统。实现的添加购物商品,删除购物商品并且显示购物车信息。

 1. 在自己建立的WEB工程中,建立包shopcart.dto,在相应的包中添加类Product.java ,ShopCart.java

  1. /*类Product */
  2. package shopcart.dto;
  3. import java.io.Serializable;
  4. public class Product implements Serializable
  5. {
  6.     private String id;//产品标识
  7.     private String name;//产品名称
  8.     private String description;//产品描述
  9.     private double price;//产品价格
  10.     public Product()
  11.     {
  12.     }
  13.     public Product(String id, String name, String description, double price)
  14.     {
  15.         this.id = id;
  16.         this.name = name;
  17.         this.description = description;
  18.         this.price = price;
  19.     }
  20.     public void setId(String id)
  21.     {
  22.         this.id = id;
  23.     }
  24.     public void setName(String name)
  25.     {
  26.         this.name = name;
  27.     }
  28.     public void setDescription(String description)
  29.     {
  30.         this.description = description;
  31.     }
  32.     public void setPrice(double price)
  33.     {
  34.         this.price = price;
  35.     }
  36.     public String getId()
  37.     {
  38.         return id;
  39.     }
  40.     public String getName()
  41.     {
  42.         return name;
  43.     }
  44.     public String getDescription()
  45.     {
  46.         return description;
  47.     }
  48.     public double getPrice()
  49.     {
  50.         return price;
  51.     }
  52. }
  53. /*类ShopCart */
  54. package shopcart.dto;
  55. import java.io.Serializable;
  56. import java.util.*;
  57. public class ShopCart implements Serializable
  58. {
  59.     public ShopCart()
  60.     {
  61.     }
  62.     private List cart = null;
  63.     /**
  64.      * 添加一个产品到购物车
  65.      * @param product Product
  66.      */
  67.     public void addProductToCart(Product product)
  68.     {
  69.         if (cart == null)
  70.             cart = new ArrayList();
  71.         Iterator it = cart.iterator();
  72.         while (it.hasNext())
  73.         {
  74.             Product item = (Product) it.next();
  75.             if (item.getId().equals(product.getId()))
  76.             {
  77.                 return;
  78.             }
  79.         }
  80.         cart.add(product);
  81.     }
  82.     /**
  83.      * 从购物车中删除一产品
  84.      * @param productId String 产品id
  85.      */
  86.     public void removeProductFromCart(String productId)
  87.     {
  88.         if (cart == null)
  89.             return;
  90.         Iterator it = cart.iterator();
  91.         while (it.hasNext())
  92.         {
  93.             Product item = (Product) it.next();
  94.             if (item.getId().equals(productId))
  95.             {
  96.                 it.remove();
  97.                 return;
  98.             }
  99.         }
  100.     }
  101.     /**
  102.      * 计算购物车中的商品价格
  103.      * @return double 商品价格总数
  104.      */
  105.     public double getAllProductPrice()
  106.     {
  107.         if (cart == null)
  108.             return 0;
  109.         double totalPrice = 0;
  110.         Iterator it = cart.iterator();
  111.         while (it.hasNext())
  112.         {
  113.             Product item = (Product) it.next();
  114.             totalPrice += item.getPrice();
  115.         }
  116.         return totalPrice;
  117.     }
  118.     /**
  119.      * 返回购物车所有产品信息
  120.      * @return List
  121.      */
  122.     public List getAllProductsFromCart()
  123.     {
  124.         return cart;
  125.     }
  126. }

2.在WebRoot目录下添加包shopCart  在里边添加ShowProductsJSP.jsp   ShoppingJSP.jsp  ShopCartJSP.jsp

ShowProductsJSP.jsp   :::::::::

  1. <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
  2. <%@ page import="shopcart.dto.*"%>
  3. <%
  4.     String path = request.getContextPath();
  5.     String basePath = request.getScheme() + "://"
  6.             + request.getServerName() + ":" + request.getServerPort()
  7.             + path + "/";
  8. %>
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  10. <html>
  11.     <head>
  12.         <base href="<%=basePath%>">
  13.         <title>My JSP 'ShowProductsJSP.jsp' starting page</title>
  14.         <meta http-equiv="pragma" content="no-cache">
  15.         <meta http-equiv="cache-control" content="no-cache">
  16.         <meta http-equiv="expires" content="0">
  17.         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  18.         <meta http-equiv="description" content="This is my page">
  19.         <!--
  20.     <link rel="stylesheet" type="text/css" href="styles.css">
  21.     -->
  22.     </head>
  23.     <body bgcolor="#ffffff">
  24.         <%
  25.             Map products = new HashMap();
  26.             products.put("001"new Product("001""mp3播放器",
  27.                     "效果很不错的mp3播放器,存储空间达1GB", 999.00));
  28.             products.put("002"new Product("002""数码相机""象素500万,10倍光学变焦",
  29.                     2500.00));
  30.             products.put("003"new Product("003""数码摄像机",
  31.                     "120万象素,支持夜景拍摄,20倍光学变焦", 5999.00));
  32.             products.put("004"new Product("004""迷你mp4",
  33.                     "市面所能见到的最好的mp4播放器,国产", 1999.99));
  34.             products.put("005"new Product("005""多功能手机",
  35.                     "集mp3播放、100万象素数码相机,手机功能于一体", 2199.99));
  36.             ServletContext context = getServletContext();
  37.         context.setAttribute("products", products);
  38.         %>
  39.         <H1>
  40.             产品显示
  41.         </H1>
  42.         <a href="/helloApp/shopCart/ShowCartJSP.jsp">查看购物车</a>
  43.         <form name="productForm" action="/helloApp/shopCart/ShoppingJSP.jsp" method="POST">
  44.             <input type="hidden" name="action" value="purchase">
  45.             <table border="1" cellspacing="0">
  46.                 <tr bgcolor="#CCCCCC">
  47.                 <tr bgcolor="#CCCCCC">
  48.                     <td>
  49.                         序号
  50.                     </td>
  51.                     <td>
  52.                         产品名称
  53.                     </td>
  54.                     <td>
  55.                         产品描述
  56.                     </td>
  57.                     <td>
  58.                         产品价格(¥)
  59.                     </td>
  60.                     <td>
  61.                         添加到购物车
  62.                     </td>
  63.                 </tr>
  64.                 <%
  65.                     Set productIdSet = products.keySet();
  66.                     Iterator it = productIdSet.iterator();
  67.                     int number = 1;
  68.             
  69.                     while (it.hasNext()) {
  70.                         String id = (String) it.next();
  71.                         Product product = (Product) products.get(id);
  72.                         %><tr>
  73.                     <td>
  74.                         <%=number++ %></td>
  75.                     <td>
  76.                         <%=product.getName()%>
  77.                     </td>
  78.                     <td><%=product.getDescription() %>
  79.                     </td>
  80.                     <td>
  81.                         <%=product.getPrice() %></td>
  82.                     <td>
  83.                         <input type="checkbox" name="productId"
  84.                             value="<%=product.getId() %>">
  85.                     </td>
  86.                 </tr>
  87.                     <% }%>
  88.             </table>
  89.             <p>
  90.                 <input type="reset" value="全部取消" />
  91.                 <input type="submit" value="确定" />
  92.             </p>
  93.         </form>
  94.     </body>
  95. </html>

ShoppingJSP.jsp::::::::::

  1. <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
  2. <%@ page import="shopcart.dto.*"%>
  3. <%
  4.     String path = request.getContextPath();
  5.     String basePath = request.getScheme() + "://"
  6.             + request.getServerName() + ":" + request.getServerPort()
  7.             + path + "/";
  8. %>
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  10. <html>
  11.     <head>
  12.         <base href="<%=basePath%>">
  13.         <title>My JSP 'shopping.jsp' starting page</title>
  14.         <meta http-equiv="pragma" content="no-cache">
  15.         <meta http-equiv="cache-control" content="no-cache">
  16.         <meta http-equiv="expires" content="0">
  17.         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  18.         <meta http-equiv="description" content="This is my page">
  19.         <!--
  20.     <link rel="stylesheet" type="text/css" href="styles.css">
  21.     -->
  22.     </head>
  23.     <body bgcolor="#ffffff">
  24.         <%
  25.             try{
  26.             response.setContentType("text/html; charset=GBK");
  27.             HttpSession mysession = request.getSession();
  28.             ServletContext context = getServletContext();
  29.             ShopCart cart = (ShopCart) mysession.getAttribute("shopCart");
  30.             String action = request.getParameter("action");
  31.             if ("remove".equals(action)) {
  32.                 String removeId = request.getParameter("removeId");
  33.                 cart.removeProductFromCart(removeId);
  34.             } else if(action.equals("purchase")){
  35.                 String[] productIds = request.getParameterValues("productId");
  36.                 Map products = (Map) context.getAttribute("products");
  37.                 if (cart == null) {
  38.                     cart = new ShopCart();
  39.                     mysession.setAttribute("shopCart", cart);
  40.                 }
  41.                 if (productIds == null) {
  42.                     productIds = new String[0];
  43.                 }
  44.                 for (int i = 0; i < productIds.length; i++) {
  45.                     Product product = (Product) products.get(productIds[i]);
  46.                     cart.addProductToCart(product);
  47.                 }
  48.             }
  49.             }catch(NullPointerException e)
  50.             {e.printStackTrace();}
  51.         %>
  52.         <jsp:forward page="/shopCart/ShowCartJSP.jsp"></jsp:forward>
  53.     </body>
  54. </html>

ShopCartJSP.jsp:::::::::

  1. <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
  2. <%@ page import="shopcart.dto.*" %>
  3. <%
  4. String path = request.getContextPath();
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  6. %>
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  8. <html>
  9.   <head>
  10.     <base href="<%=basePath%>">
  11.     
  12.     <title>My JSP 'ShowCartJSP.jsp' starting page</title>
  13.     
  14.     <meta http-equiv="pragma" content="no-cache">
  15.     <meta http-equiv="cache-control" content="no-cache">
  16.     <meta http-equiv="expires" content="0">    
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  18.     <meta http-equiv="description" content="This is my page">
  19.     <!--
  20.     <link rel="stylesheet" type="text/css" href="styles.css">
  21.     -->
  22.   </head>
  23.   
  24.   <body><% 
  25.   HttpSession mysession = request.getSession();
  26.         ShopCart cart = (ShopCart) mysession.getAttribute("shopCart");
  27.     List products = null;
  28.             if (cart == null
  29.                     || (products = cart.getAllProductsFromCart()) == null) {
  30.         %>
  31.         
  32.         <h1>
  33.             你目前没有购买任何产品
  34.         </h1>
  35.         
  36.         <p>
  37.             <a href="/shopCart/ShowProductsJSP.jsp">返回产品显示页</a>
  38.         </p>
  39.         <%
  40.             } else {
  41.                 Iterator iterator = products.iterator();
  42.         %>
  43.         
  44.         <h1>
  45.             你目前购买的产品为:
  46.         </h1>
  47.         
  48.         <table border="1" cellspace="0">
  49.             <tr bgcolor="#CCCCCC">
  50.                 <td>
  51.                     产品名称
  52.                 </td>
  53.                 <td>
  54.                     产品描述
  55.                 </td>
  56.                 <td>
  57.                     价格
  58.                 </td>
  59.                 <td>
  60.                     操作
  61.                 </td>
  62.             </tr>
  63.             <%
  64.                 while (iterator.hasNext()) {
  65.                         Product productItem = (Product) iterator.next();
  66.             %>
  67.             <tr>
  68.                 <td>
  69.                     <%=productItem.getName()%>
  70.                 </td>
  71.                 <td><%=productItem.getDescription()%>
  72.                 </td>
  73.                 <td>
  74.                     <%=productItem.getPrice()%></td>
  75.                 <td>
  76.                     <a
  77.                         href="/helloApp/shopCart/ShoppingJSP.jsp?action=remove&removeId=<%=productItem.getId()%>">删除</a>
  78.                 </td>
  79.             </tr>
  80.             <%
  81.                 }
  82.             %>
  83.         </table>
  84.         <p>
  85.             目前您购物车的总价格为:<%=cart.getAllProductPrice()%>
  86.             元人民币。
  87.         </p>
  88.         <p>
  89.             </br>
  90.             <a href="/helloApp/shopCart/ShowProductsJSP.jsp">返回产品显示页</a>
  91.         </p>
  92.         <%
  93.             }
  94.         %>
  95.   </body>
  96. </html>

3.最后打开Tomcat,在浏览器URL中输入http://localhost:8088/helloApp/shopCart/ShowProductsJSP.jsp即可!

 

 


http://chatgpt.dhexx.cn/article/bDcUX8Tw.shtml

相关文章

JSP | 简易购物车的实现

本程序共包含四部分文件&#xff0c;只包含jsp文件&#xff0c;将java代码嵌入到jsp文件中实现&#xff1a; loginID.jsp、shop.jsp、food.jsp、count.jsp&#xff0c;分别实现的功能是输入ID页面、超链接功能选择页面、购物页面和购物车页面。 login.jsp文件代码如下&#x…

Javaweb-购物商城实现展示商品,实现购物车购物,结算(Servlet+mysql+jsp+tomcat)

演示视频: 购物网站 代码: https://github.com/wu1369955/shopping 购物网站首页 首先说明:这个是花几天搭建出来玩的,从github上拉到找好看的框架组合的,效果还不错,主要是学习作用.源码之类的也会分享出来,希望一起进步,最好动手实践,可以参照逻辑做的更好, 简易购物商城设…

javascript购物车实现详细代码讲解

我们肯定都很熟悉商品购物车这一功能&#xff0c;每当我们在某宝某东上购买商品的时候&#xff0c;看中了哪件商品&#xff0c;就会加入购物车中&#xff0c;最后结算。购物车这一功能&#xff0c;方便消费者对商品进行管理&#xff0c;可以添加商品&#xff0c;删除商品&#…

JSP(6)简单购物车实现

两个jsp页面之间跳转需要通过Servlet控制器实现 1 创建数据库表 -- 创建一个序列&#xff0c;使book的id自增长 create sequence book_seq start with 1 increment by 1 minvalue 1 maxvalue 99999999999999 nocycle nocache -- 创建一个序列&#xff0c;使orders的id自增长…

用JSP实现的商城购物车模块

这两天&#xff0c;在学习JSP&#xff0c;正好找个小模块来练练手&#xff1a; 以下就是实现购物车模块的页面效果截图&#xff1a; 图1. 产品显示页面 通过此页面进行产品选择&#xff0c;加入到购物车 图2 .购物车页面 图3 . 商品数量设置 好了&#xff0c;先不贴图了&#x…

项目-Java Web购物车-JSP实现

文章目录 源码地址界面演示目录结构准备工作登录注册功能定义DaoBaseDaoProductDaoImp 定义EntityProductCartItem 商品列表与详情界面★购物车页面更多 源码地址 点击文章底部打赏后私信发送源码地址 界面演示 介绍(旧) 浏览器打开http://localhost:8080/ShopCart/login.j…

jsp购物车(session版)

目录 一、要用到的 js 和 css 文件 二、数据库表实体类Dao类 1、数据库表 2、实体类 3、Dao类方法(GoodsDao) 四、商品主页面&#xff08;index.jsp&#xff09; 1、代码分析 2、完整代码 五、购物车页面&#xff08;spcar.jsp&#xff09; 1、代码分析 2、完整代码 3、删除…

jsp实现购物车

文章目录 一、案例演示二、实现步骤1.info.jsp2.UserServlet3.cart.jsp4.UserServlet 一、案例演示 点击购买进入到购物车页面&#xff0c;增加减少数量都会对小计&#xff0c;总计产生影响&#xff0c;点击继续购买&#xff0c;回到挑选页面product_list.jsp 二、实现步骤 …

图像识别技术

图像识别算法实现收藏 <script type"text/javascript"></script> <script></script> 以往的图像处理函数实现&#xff0c;多是针对图像句柄。算法实现 需要操作复杂的图像文件。 但是&#xff0c;这种方式算法实现和调试的周期比较长。为了加…

神经网络图像识别算法,神经网络提取特征值

哪些神经网络可以用在图像特征提取上 BP神经网络、离散Hopfield网络、LVQ神经网络等等都可以。 1.BP&#xff08;BackPropagation&#xff09;神经网络是1986年由Rumelhart和McCelland为首的科学家小组提出&#xff0c;是一种按误差逆传播算法训练的多层前馈网络&#xff0c;…

基于神经网络的图像识别

一、目的 1. 了解深度学习的基本原理&#xff1b; 2. 能够使用 PaddleHub 深度学习开源工具进行图像识别&#xff1b; 3. 能够使用 PaddlePaddle 训练神经网络模型&#xff1b; 4. 能够使用 Paddle 完成手写实验. 5. 能够使用 keras 训练神经网络模型。 二、环境配置&#xff1…

机器学习决策树算法解决图像识别

算法介绍 什么是决策树算法 决策树又称判定树&#xff0c;是一个类似于流程图的树结构&#xff1a;其中&#xff0c;每个内部结点表示在一个属性上的测试&#xff0c;每个分支代表一个属性输出&#xff0c;而每个树叶结点代表类或类分布。树的最顶层是根结点。 构造决策树的基…

opencv 图像识别

opencv的目标是使计算机可以快速准确地从数字图像中提取和分析特征。它使用了许多新的算法和技术&#xff0c;例如改进的模板匹配、基于统计的特征分析以及深度学习等。opencv支持多种平台&#xff0c;包括 Windows、 MacOS、 Linux和 Android&#xff0c;开发者可以使用 OpenC…

图像识别算法(二)

一、线性分类器 线性分类器&#xff08;Linear Classification&#xff09;是比KNN分类器更有效的一种分类器。这个方法有两个重要的部分&#xff1a;分数函数&#xff08;score function&#xff09;和损失函数&#xff08;loss function&#xff09;。分数函数是江原始数据匹…

基于KNN算法的图像识别

你需要完成一个图像识别的任务&#xff0c;主要使用的模型是KNN算法。使用的数据集是cifar-10&#xff0c;是图像识别领域最为经典的数据及之一。具体的数据可以从以下的链接下载&#xff1a; https://www.cs.toronto.edu/~kriz/cifar.html&#xff0c; 下载之后把是数据集解压…

opencv图像算法

图像的对比度增强 一&#xff1a; 绘制直方图 就是把各个像素值所含有的个数统计出来&#xff0c;然后画图表示。 可以看到在当前图像中&#xff0c;哪个像素值的个数最多。 同时&#xff0c;可以看当前图像总体的像素值大小在哪些范围。。靠近0的话&#xff0c;说明图像偏暗…

图像识别中的深度学习

图像识别中的深度学习 来源&#xff1a;《中国计算机学会通讯》第8期《专题》 作者&#xff1a;王晓刚 深度学习发展历史 深度学习是近十年来人工智能领域取得的重要突破。它在语音识别、自然语言处理、计算机视觉、图像与视频分析、多媒体等诸多领域的应用取得了巨大成功。现有…

图像识别

图像识别主要用到了两个第三方的iOS框架&#xff1a;OpenCV和TesseractOCR&#xff0c;OpenCV用来做图像处理&#xff0c;定位到身份证号码的区域&#xff0c;TesseractOCR则是对定位到的区域内的内容进行识别。 OpenCV中的一些简单的处理图像的方法&#xff1a;灰度处理、二…

深度学习与图像识别 图像检测

主要做了基于深度学习的图像识别与检测的研究&#xff0c;下面是一些整理内容 1、深度学习的优势 &#xff08;1&#xff09;从统计&#xff0c;计算的角度看&#xff0c;DL特别适合处理大数据 a、用较为复杂的模型降低模型偏差 b、用大数据提升统计估计的准确度 c、用可扩展的…

人工智能-图像识别

图像识别技术是信息时代的一门重要的技术&#xff0c;其产生目的是为了让计算机代替人类去处理大量的物理信息。随着计算机技术的发展&#xff0c;人类对图像识别技术的认识越来越深刻。图像识别技术的过程分为信息的获取、预处理、特征抽取和选择、分类器设计和分类决策。简单…