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

article/2025/10/17 22:58:34

 

这两天,在学习JSP,正好找个小模块来练练手:

    以下就是实现购物车模块的页面效果截图:

图1. 产品显示页面 通过此页面进行产品选择,加入到购物车

 

图2 .购物车页面

 

图3 . 商品数量设置

 

好了,先不贴图了,直接上代码;先看看项目的文档结构把(麻雀虽小,五脏俱全):

 

整个项目包含三个类,两个JSP页面,下面分别把他们的代码贴上:

 

Cart.java

package shopping.cart;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Cart {
List<CartItem> items = new ArrayList<CartItem>();
public List<CartItem> getItems() {
return items;
}
public void setItems(List<CartItem> items) {
this.items = items;
}
public void add(CartItem ci) {
for (Iterator<CartItem> iter = items.iterator(); iter.hasNext();) {
CartItem item = iter.next();
if(item.getProduct().getId() == ci.getProduct().getId()) {
item.setCount(item.getCount() + 1);
return;
}
}
items.add(ci);
}
public double getTotalPrice() {
double d = 0.0;
for(Iterator<CartItem> it = items.iterator(); it.hasNext(); ) {
CartItem current = it.next();
d += current.getProduct().getPrice() * current.getCount();
}
return d;
}
public void deleteItemById(String productId) {
for (Iterator<CartItem> iter = items.iterator(); iter.hasNext();) {
CartItem item = iter.next();
if(item.getProduct().getId().equals(productId)) {
iter.remove();
return;
}
}
}
}


 

CartItem.java

 

package shopping.cart;
import shopping.cart.Product;
public class CartItem {
private Product product;
private int count;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
}


 

 Product.java

package shopping.cart;
import java.io.Serializable;
public class Product implements Serializable {
private String id;// 产品标识
private String name;// 产品名称
private String description;// 产品描述
private double price;// 产品价格
public Product() {
}
public Product(String id, String name, String description, double price) {
this.id = id;
this.name = name;
this.description = description;
this.price = price;
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setDescription(String description) {
this.description = description;
}
public void setPrice(double price) {
this.price = price;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public double getPrice() {
return price;
}
}


 

下面是俩JSP页面源码:

ShowProducts.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ page import="shopping.cart.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'ShowProductsJSP.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body bgcolor="#ffffff">
<%
Map products = new HashMap();
products.put("1", new Product("1", "mp3播放器",
"效果很不错的mp3播放器,存储空间达1GB", 100.00));
products.put("2", new Product("2", "数码相机", "象素500万,10倍光学变焦",
500.00));
products.put("3", new Product("3", "数码摄像机",
"120万象素,支持夜景拍摄,20倍光学变焦", 200.00));
products.put("4", new Product("4", "迷你mp4",
"市面所能见到的最好的mp4播放器,国产", 300.00));
products.put("5", new Product("5", "多功能手机",
"集mp3播放、100万象素数码相机,手机功能于一体", 400.00));
products.put("6", new Product("6", "多功能手机111",
"集mp3播放23、100万象素数码相机111,手机功能于一体111",600.00));
products.put("7", new Product("7", "11111111111",
"集mp3播放23、100万象素数码相机111,手机功能于一体111",700.00));
products.put("8", new Product("8", "2222222222",
"集mp3播放23、100万象素数码相机111,手机功能于一体111",800.00));
products.put("9", new Product("9", "33333333333333",
"集mp3播放23、100万象素数码相机111,手机功能于一体111",900.00));
session.setAttribute("products", products);
%>
<H1>
产品显示
</H1>
<form name="productForm"
action="http://localhost:8088/JSPlearning/ShopCartJSP.jsp"
method="POST">
<input type="hidden" name="action" value="purchase">
<table border="1" cellspacing="0">
<tr bgcolor="#CCCCCC">
<tr bgcolor="#CCCCCC">
<td>
序号
</td>
<td>
产品名称
</td>
<td>
产品描述
</td>
<td>
产品单价(¥)
</td>
<td>
添加到购物车
</td>
</tr>
<%
Set productIdSet = products.keySet();
Iterator it = productIdSet.iterator();
int number = 1;
while (it.hasNext()) {
String id = (String) it.next();
Product product = (Product) products.get(id);
%><tr>
<td>
<%=number++%>
</td>
<td>
<%=product.getName()%>
</td>
<td><%=product.getDescription()%>
</td>
<td>
<%=product.getPrice()%></td>
<td>
<a href="Buy.jsp?id=<%=product.getId()%>&action=add" target="cart">我要购买</a>
</td>
</tr>
<%
}
%>
</table>
<p>
</p>
</form>
</body>
</html>


 

Buy.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ page import="shopping.cart.*" %>
<%
Cart c = (Cart)session.getAttribute("cart");
if(c == null) {
c = new Cart();
session.setAttribute("cart", c);
}
double totalPrice = c.getTotalPrice();
request.setCharacterEncoding("GBK");
String action = request.getParameter("action");
Map products = (HashMap)session.getAttribute("products");
if(action != null && action.trim().equals("add")) {
String id = request.getParameter("id");
Product p = (Product)products.get(id);
CartItem ci = new CartItem();
ci.setProduct(p);
ci.setCount(1);
c.add(ci);
}
if(action != null && action.trim().equals("delete")) {
String id = request.getParameter("id");
c.deleteItemById(id);
}
if(action != null && action.trim().equals("update")) {
for(int i=0; i<c.getItems().size(); i++) {
CartItem ci = c.getItems().get(i);
int count = Integer.parseInt(request.getParameter("p" + ci.getProduct().getId()));
ci.setCount(count);
}
}
%> 
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
List<CartItem> items = c.getItems();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>购物车</title>
</head>
<body>
<!-- c的值是:<%=(c == null) %> items的值是:<%=(items == null) %> -->
<form action="Buy.jsp" method="get">
<input type="hidden" name="action" value="update"/>
<table align="center" border="1">
<tr>
<td>产品ID</td>
<td>产品名称</td>
<td>购买数量</td>
<td>单价</td>
<td>总价</td>
<td>处理</td>
</tr>
<%
for(Iterator<CartItem> it = items.iterator(); it.hasNext(); ) {
CartItem ci = it.next();
%>
<tr>
<td><%=ci.getProduct().getId() %></td>
<td><%=ci.getProduct().getName() %></td>
<td>
<input type="text" size=3 name="<%="p" + ci.getProduct().getId() %>" value="<%=ci.getCount() %>" 
οnkeypress="if (event.keyCode < 45 || event.keyCode > 57) event.returnValue = false;"
οnchange="document.forms[0].submit()">				
</td>
<td><%=ci.getProduct().getPrice() %></td>
<td><%=ci.getProduct().getPrice()*ci.getCount() %></td>
<td>
<a href="Buy.jsp?action=delete&id=<%=ci.getProduct().getId() %>">删除</a>
</td>
</tr>
<%
}
%>
<tr>
<td colspan=3 align="right">
所有商品总价格为:
</td>
<td colspan=3><%=c.getTotalPrice() %></td>
</tr>
<tr>
<!--	<td colspan=3>
<a href="javascript:document.forms[0].submit()">修改</a> 
</td>  -->
<td colspan=6 align="right">
<a href="">下单</a>		
</td>
</tr>
</table>
</form>
</body>
</html>


 

配置好相关文件,在tomcat中发布后,在浏览器中输入http://localhost:8088/shopCart/ShowProducts.jsp 即可进入产品展示页面,其它操作都可以在页面上完成!

注意:我使用的tomcat端口(8088)被自己改过,如果没改过tomcat端口的童鞋,默认端口为8080。

 

 


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

相关文章

项目-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;人类对图像识别技术的认识越来越深刻。图像识别技术的过程分为信息的获取、预处理、特征抽取和选择、分类器设计和分类决策。简单…

神经网络图像识别算法,神经网络图像识别技术

目前进行图像处理,通常使用什么神经网络 谷歌人工智能写作项目&#xff1a;小发猫 数字图像处理的主要方法 数字图像处理的工具可分为三大类&#xff1a;第一类包括各种正交变换和图像滤波等方法&#xff0c;其共同点是将图像变换到其它域&#xff08;如频域&#xff09;中进…

人工智能中的图像识别技术

点击上方“小白学视觉”&#xff0c;选择加"星标"或“置顶” 重磅干货&#xff0c;第一时间送达 伴随着图像处理技术的飞速发展&#xff0c;推动了图像识别技术的产生和发展&#xff0c;并逐渐成为人工智能领域中重要的组成部分&#xff0c;并广泛地运用于面部识别、…

图像处理那些算法

图像的几何变换 1&#xff09;旋转 借助矩阵运算来实现图像的旋转功能 2&#xff09;平移 借助矩阵运算来实现图像的旋转功能 3&#xff09;对称 借助矩阵运算来实现图像的对称功能 水平镜像变换&#xff1a; 垂直镜像变换&#xff1a; 图片裁剪 借助 get (gca, ‘cu…

最新开源的图像识别算法来了!

Datawhale开源 方向&#xff1a;图像识别开源项目 人脸、车辆、人体属性、卡证、交通标识等经典图像识别能力&#xff0c;在我们当前数字化工作及生活中发挥着极其重要的作用。业内也不乏顶尖公司提供的可直接调用的API、SDK&#xff0c;但这些往往面临着定制化场景泛化效果不…

神经网络的图像识别技术,神经网络图像识别算法

神经网络提取图像的概率分布特征 神经网络提取图像的概率分布特征&#xff1a;由于一个映射面上的神经元共享权值&#xff0c;因而减少了网络自由参数的个数&#xff0c;降低了网络参数选择的复杂度。 卷积神经网络中的每一个特征提取层&#xff08;C-层&#xff09;都紧跟着…