jsp购物车(session版)

article/2025/10/17 23:03:43

目录

一、要用到的 js 和 css 文件

二、数据库表+实体类+Dao类

        1、数据库表

        2、实体类

        3、Dao类方法(GoodsDao)

四、商品主页面(index.jsp)

        1、代码分析

        2、完整代码

五、购物车页面(spcar.jsp)

        1、代码分析

        2、完整代码

        3、删除方法(dodelete.jsp)


 
     session为每个浏览器独创一个区域,有一个key,通过key可以找到独享的区域

下面这个代码利用的就是session,每个购物车是一个map集合,然后把这个map集合放到session中作为每个浏览器独享的区域

一、要用到的 js 和 css 文件

       css文件:bootstrap.min.css 或 bootstrap.css 

        js文件:bootstrap.js

二、数据库表+实体类+Dao类

        1、数据库表

 

        2、实体类

商品类(Goods)
gid商品编号
gname商品名称
gprice商品单价
ginfo商品描述
gface商品图片
小订单(OrderItem)
Goods 类型(对象) 商品对象
gnumber商品数量
gnumprice商品总价

        3、Dao类方法(GoodsDao)

                查询所有商品方法:在页面显示所有商品

	/*** 查询所有商品* @return 返回商品集合*/public ArrayList<Goods> getAll(){ArrayList<Goods> glist=new ArrayList<>();Connection con=null;PreparedStatement ps=null;ResultSet rs=null;try {con=DBHelper.getCon();String sql="select * from Goods";ps=con.prepareStatement(sql);rs=ps.executeQuery();while(rs.next()) {glist.add(new Goods(rs.getInt(1), rs.getString(2), rs.getDouble(3), rs.getString(4), rs.getString(5)));}} catch (Exception e) {e.printStackTrace();} finally {DBHelper.closeDb(con, ps, rs);}return glist;}

                查询单个商品:根据编号查询到商品所有信息

	/*** 查询单个* @param gid* @return*/public Goods getById(int gid){Goods g=null;Connection con=null;PreparedStatement ps=null;ResultSet rs=null;try {con=DBHelper.getCon();String sql="select * from Goods where gid="+gid;ps=con.prepareStatement(sql);rs=ps.executeQuery();if(rs.next()) {g=new Goods(rs.getInt(1), rs.getString(2), rs.getDouble(3), rs.getString(4), rs.getString(5));}} catch (Exception e) {e.printStackTrace();} finally {DBHelper.closeDb(con, ps, rs);}return g;}

四、商品主页面(index.jsp)

        1、代码分析

调用Dao类查询所有的方法,遍历集合,给页面绑定数据

		<%GoodsDao gd=new GoodsDao();ArrayList<Goods> glist=gd.getAll();for(Goods g:glist){%><tr><td><%=g.getGid() %></td><td><%=g.getGname() %></td><td><%=g.getGprice() %></td><td><%=g.getGinfo() %></td><td><img alt="" src="<%=g.getGface()%>"></td><td><button onclick="gm(<%=g.getGid()%>)" class="btn btn-info">加入购物车</button></td></tr><%} %>

        2、完整代码

              index.jsp

<%@page import="entity.Goods"%>
<%@page import="java.util.ArrayList"%>
<%@page import="dao.GoodsDao"%>
<%@ page language="java" contentType="text/html; charset=utf-8"pageEncoding="utf-8"%>
<!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=utf-8">
<title>商品主页</title>
<!-- 引用jQuery库 -->
<script type="text/javascript" src="jquery-3.3.1.js"></script>
<script type="text/javascript" src="js/bootstrap.js"></script>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<script type="text/javascript">function gm(gid) {//跳转到do页面 顺便传个编号location.href="dogwc.jsp?gid="+gid;}
</script>
</head>
<body><table class="table table-hover"><tr><td>商品编号</td><td>商品名称</td><td>商品单价</td><td>商品介绍</td><td>商品图片</td><td>操作</td></tr><%GoodsDao gd=new GoodsDao();ArrayList<Goods> glist=gd.getAll();for(Goods g:glist){%><tr><td><%=g.getGid() %></td><td><%=g.getGname() %></td><td><%=g.getGprice() %></td><td><%=g.getGinfo() %></td><td><img alt="" src="<%=g.getGface()%>"></td><td><button onclick="gm(<%=g.getGid()%>)" class="btn btn-info">加入购物车</button></td></tr><%} %></table></body>
</html>

五、购物车页面(spcar.jsp)

        1、代码分析

获取到session中的订单集合,遍历集合获取到集合内的值,绑定到页面

	<%//获取到session中的订单集合ArrayList<OrderItem> olist=(ArrayList<OrderItem>)session.getAttribute("olist");//把订单绑定到页面for(int i=0;i<olist.size();i++){%><tr><td><img alt="" src="<%=olist.get(i).getGoods().getGface() %>"></td><td><%=olist.get(i).getGoods().getGname() %></td><td><%=olist.get(i).getGoods().getGprice() %></td><td><%=olist.get(i).getGoods().getGinfo() %></td><td><button class="btn" onclick="j('-',<%=olist.get(i).getGoods().getGid()%>)">-</button><input id="<%=olist.get(i).getGoods().getGid()%>" onblur="xg(this,<%=olist.get(i).getGoods().getGid() %>)" style="width:40px;text-align:center;" type="text" value="<%=olist.get(i).getGnumber() %>"><button class="btn" onclick="j('+',<%=olist.get(i).getGoods().getGid()%>)">+</button></td><td><%=olist.get(i).getSumPrice() %></td><td><a href="dodelete.jsp?gid=<%=olist.get(i).getGoods().getGid()%>"><span class="glyphicon glyphicon-trash"></span></a></td></tr><%} %></table>

文本框修改商品数量

        跳转到dogwc.jsp对商品的数量,小订单总价,所有订单总价进行修改

	function xg(obj,gid) {var gnumber = obj.value;location.href="dogwc.jsp?gid="+gid+"&gnum="+gnumber;}

在input标签中给方法传值(onblur焦点消失事件,焦点消失后,商品的总价随数量加减)

<input id="<%=olist.get(i).getGoods().getGid()%>" onblur="xg(this,<%=olist.get(i).getGoods().getGid() %>)" style="width:40px;text-align:center;" type="text" value="<%=olist.get(i).getGnumber() %>">

点击 加“+” 减“-” 对商品的数量进行加减

        判断值:减到小于1的值,直接复制等于1

        判断完后,跳转到dogwc.jsp对商品的数量,小订单总价,所有订单总价进行修改

 	function $(id){return document.getElementById(id);} function j(j,gid){var gnumber=$(gid).value;if(j=="-"){gnumber--;if(gnumber<1){gnumber=1;/* location.href="dodelete.jsp?gid="+gid+"&gnum="+gnumber; */}location.href="dogwc.jsp?gid="+gid+"&gnum="+gnumber;}else if(j=="+"){gnumber++;location.href="dogwc.jsp?gid="+gid+"&gnum="+gnumber;}}

        给按钮添加点击事件onclick,并传值

<button class="btn" onclick="j('-',<%=olist.get(i).getGoods().getGid()%>)">-</button><button class="btn" onclick="j('+',<%=olist.get(i).getGoods().getGid()%>)">+</button>

       在dogwc.jsp对订单总价的计算用session传值 

       session.getAttribute("sumPrice")

<button class="btn btn-success">总价:<%=session.getAttribute("sumPrice")%></button>
	double sumPrice = 0;for(OrderItem oit:olist){sumPrice+=oit.getSumPrice();}	//把集合放到session中session.setAttribute("olist", olist);session.setAttribute("sumPrice", sumPrice);

        2、完整代码

            购物车页面(spcar.jsp)

<%@page import="entity.OrderItem"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=utf-8"pageEncoding="utf-8"%>
<!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=utf-8">
<title>购物车主页</title>
<!-- 引用jQuery库 -->
<script type="text/javascript" src="js/jquery-3.3.1.js"></script>
<script type="text/javascript" src="js/bootstrap.js"></script>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>
<script type="text/javascript">function xg(obj,gid) {var gnumber = obj.value;location.href="dogwc.jsp?gid="+gid+"&gnum="+gnumber;}function $(id){return document.getElementById(id);} function j(j,gid){var gnumber=$(gid).value;if(j=="-"){gnumber--;if(gnumber<1){gnumber=1;/* location.href="dodelete.jsp?gid="+gid+"&gnum="+gnumber; */}location.href="dogwc.jsp?gid="+gid+"&gnum="+gnumber;}else if(j=="+"){gnumber++;location.href="dogwc.jsp?gid="+gid+"&gnum="+gnumber;}}
</script>
</head>
<body><h1 align="center"><a href="index.jsp"><span class="glyphicon glyphicon-home"></span></a></h1><table class="table table-hover"><tr><td>商品图片</td><td>商品名称</td><td>商品单价</td><td>商品介绍</td><td>商品数量</td><td>订单总价</td><td><span class="glyphicon glyphicon-cog"></span></td></tr><%//获取到session中的订单集合ArrayList<OrderItem> olist=(ArrayList<OrderItem>)session.getAttribute("olist");//把订单绑定到页面for(int i=0;i<olist.size();i++){%><tr><td><img alt="" src="<%=olist.get(i).getGoods().getGface() %>"></td><td><%=olist.get(i).getGoods().getGname() %></td><td><%=olist.get(i).getGoods().getGprice() %></td><td><%=olist.get(i).getGoods().getGinfo() %></td><td><button class="btn" onclick="j('-',<%=olist.get(i).getGoods().getGid()%>)">-</button><input id="<%=olist.get(i).getGoods().getGid()%>" onblur="xg(this,<%=olist.get(i).getGoods().getGid() %>)" style="width:40px;text-align:center;" type="text" value="<%=olist.get(i).getGnumber() %>"><button class="btn" onclick="j('+',<%=olist.get(i).getGoods().getGid()%>)">+</button></td><td><%=olist.get(i).getSumPrice() %></td><td><a href="dodelete.jsp?gid=<%=olist.get(i).getGoods().getGid()%>"><span class="glyphicon glyphicon-trash"></span></a></td></tr><%} %></table><p align="right" style="margin-right:40px"><button class="btn btn-success">总价:<%=session.getAttribute("sumPrice") %></button></p>
</body>
</html>

          dogwc.jsp

<%@page import="dao.GoodsDao"%>
<%@page import="entity.OrderItem"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%//获取商品订单的数量String number = request.getParameter("gnum");int count = 1;if(number!=null){count= Integer.valueOf(number);}//接收商品的编号String id = request.getParameter("gid");int gid = Integer.valueOf(id);//构造小订单对象OrderItem oi = new OrderItem();//给属性赋值oi.setGoods(new GoodsDao().getById(gid));//订单中的商品数量oi.setGnumber(1);//订单总价oi.setSumPrice();//获取session中的订单集合ArrayList<OrderItem> olist = (ArrayList<OrderItem>)session.getAttribute("olist");if(olist==null){//创建订单集合olist = new ArrayList<OrderItem>();}boolean b = true;//表示默认 没有相同的订单//遍历订单集合,判断是否已存在相同商品订单for(int i=0;i<olist.size();i++){if(gid==olist.get(i).getGoods().getGid()){//number为空说明是从index页面过来的if(number==null){//修改数量:原来的数量+1olist.get(i).setGnumber(olist.get(i).getGnumber()+1);//修改总价olist.get(i).setSumPrice();}else{//number不为空 说明是 从spcar页面过来的//修改数量:原来的数量修改为countolist.get(i).setGnumber(count);//修改总价olist.get(i).setSumPrice();}//表示有相同订单b = false;}}if(b){//把订单放到ArrayList集合中olist.add(oi);}//遍历订单集合double sumPrice = 0;for(OrderItem oit:olist){sumPrice+=oit.getSumPrice();}	//把集合放到session中session.setAttribute("olist", olist);session.setAttribute("sumPrice", sumPrice);//跳转页面response.sendRedirect("spcar.jsp");//request.getRequestDispatcher("spcar.jsp").forward(request, response);%>

        3、删除方法(dodelete.jsp)

              这里最好是用for循环  

<%@page import="dao.GoodsDao"%>
<%@page import="entity.OrderItem"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%//接收商品的编号String id = request.getParameter("gid");int gid = Integer.valueOf(id);//获取session中的集合ArrayList<OrderItem> olist = (ArrayList<OrderItem>)session.getAttribute("olist");for(OrderItem oi:olist){if(oi.getGoods().getGid()==gid){//根据id找商品olist.remove(oi);break;}}response.sendRedirect("spcar.jsp");%>

“少年心动是仲夏夜的荒原

        割不完 烧不尽

   长风一吹野草就连了天”


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

相关文章

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;都紧跟着…

相似图像识别算法是什么,机器图像识别常用算法

计算图像相似度的算法有哪些 SIM Structural SIMilarity&#xff08;结构相似性&#xff09;&#xff0c;这是一种用来评测图像质量的一种方法。 由于人类视觉很容易从图像中抽取出结构信息,因此计算两幅图像结构信息的相似性就可以用来作为一种检测图像质量的好坏.首先结构…

机器图像识别常用算法,图像对比识别技术

图像识别算法都有哪些 图像识别算法&#xff1a;1人脸识别类&#xff08;Eigenface&#xff0c;Fisherface算法特别多&#xff09;&#xff0c;人脸检测类&#xff08;j-v算法&#xff0c;mtcnn)2车牌识别类&#xff0c;车型识别类&#xff08;cnn&#xff09;3字符识别&#…