html简单购物车,用jsp实现一个简单的购物车web应用系统。
1. 在自己建立的WEB工程中,建立包shopcart.dto,在相应的包中添加类Product.java ,ShopCart.java
1./*类Product */
2.3.package shopcart.dto;
4.5.import java.io.Serializable;
6.7.public class Product implements Serializable
8.{
9. private String id;//产品标识 10. private String name;//产品名称 11. private String description;//产品描述 12. private double price;//产品价格 13.14. public Product()
15. {
16. }
17.18. public Product(String id, String name, String description, double price)
19. {
20. this.id = id;
21. this.name = name;
22. this.description = description;
23. this.price = price;
24. }
25.26. public void setId(String id)
27. {
28. this.id = id;
29. }
30.31. public void setName(String name)
32. {
33. this.name = name;
34. }
35.36. public void setDescription(String description)
37. {
38. this.description = description;
39. }
40.41. public void setPrice(double price)
42. {
43. this.price = price;
44. }
45.46. public String getId()
47. {
48. return id;
49. }
50.51. public String getName()
52. {
53. return name;
54. }
55.56. public String getDescription()
57. {
58. return description;
59. }
60.61. public double getPrice()
62. {
63. return price;
64. }
65.}
66./*类ShopCart */
67.68.package shopcart.dto;
69.70.import java.io.Serializable;
71.import java.util.*;
72.73.public class ShopCart implements Serializable
74.{
75. public ShopCart()
76. {
77. }
78.79. private List cart = null;
80.81. /**
82. * 添加一个产品到购物车
83. * @param product Product
84. */
85. public void addProductToCart(Product product)
86. {
87. if (cart == null)
88. cart = new ArrayList();
89. Iterator it = cart.iterator();
90. while (it.hasNext())
91. {
92. Product item = (Product) it.next();
93. if (item.getId().equals(product.getId()))
94. {
95. return;
96. }
97. }
98. cart.add(product);
99. }
100.101. /**
102. * 从购物车中删除一产品
103. * @param productId String 产品id
104. */
105. public void removeProductFromCart(String productId)
106. {
107. if (cart == null)
108. return;
109. Iterator it = cart.iterator();
110. while (it.hasNext())
111. {
112. Product item = (Product) it.next();
113. if (item.getId().equals(productId))
114. {
115. it.remove();
116. return;
117. }
118. }
119. }
120.121. /**
122. * 计算购物车中的商品价格
123. * @return double 商品价格总数
124. */
125. public double getAllProductPrice()
126. {
127. if (cart == null)
128. return 0;
129. double totalPrice = 0;
130. Iterator it = cart.iterator();
131. while (it.hasNext())
132. {
133. Product item = (Product) it.next();
134. totalPrice += item.getPrice();
135. }
136. return totalPrice;
137. }
138.139. /**
140. * 返回购物车所有产品信息
141. * @return List
142. */
143. public List getAllProductsFromCart()
144. {
145. return cart;
146. }
147.}
148.149.2.在WebRoot目录下添加包shopCart 在里边添加ShowProductsJSP.jsp ShoppingJSP.jsp ShopCartJSP.jsp
ShowProductsJSP.jsp :::::::::
1.
2.
3.
4. String path = request.getContextPath();
5. String basePath = request.getScheme() + "://"
6. + request.getServerName() + ":" + request.getServerPort()
7. + path + "/";
8.%>
9.10.
11.
12.
13.
14.15.
My JSP 'ShowProductsJSP.jsp' starting page
16.17.
18.
19.
20.
21.
22.
25.26.
27.28.
29.
30. Map products = new HashMap();
31. products.put("001", new Product("001", "mp3播放器",
32. "效果很不错的mp3播放器,存储空间达1GB", 999.00));
33. products.put("002", new Product("002", "数码相机", "象素500万,10倍光学变焦",
34. 2500.00));
35. products.put("003", new Product("003", "数码摄像机",
36. "120万象素,支持夜景拍摄,20倍光学变焦", 5999.00));
37. products.put("004", new Product("004", "迷你mp4",
38. "市面所能见到的最好的mp4播放器,国产", 1999.99));
39. products.put("005", new Product("005", "多功能手机",
40. "集mp3播放、100万象素数码相机,手机功能于一体", 2199.99));
41. ServletContext context = getServletContext();
42. context.setAttribute("products", products);
43. %>
44.
45. 产品显示
46.
47. 查看购物车
48.49.
50.
51.
52.
53.
54.
55. 序号
56.
57.
58. 产品名称
59.
60.
61. 产品描述
62.
63.
64. 产品价格(¥)
65.
66.
67. 添加到购物车
68.
69.
70.
71. Set productIdSet = products.keySet();
72. Iterator it = productIdSet.iterator();
73. int number = 1;
74.
75. while (it.hasNext()) {
76. String id = (String) it.next();
77. Product product = (Product) products.get(id);
78. %>
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90. value="">
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.ShoppingJSP.jsp::::::::::
1.
2.
3.
4. String path = request.getContextPath();
5. String basePath = request.getScheme() + "://"
6. + request.getServerName() + ":" + request.getServerPort()
7. + path + "/";
8.%>
9.10.
11.
12.
13.
14.15.
My JSP 'shopping.jsp' starting page
16.17.
18.
19.
20.
21.
22.
25.26.
27.28.
29.
30. try{
31. response.setContentType("text/html; charset=GBK");
32. HttpSession mysession = request.getSession();
33. ServletContext context = getServletContext();
34. ShopCart cart = (ShopCart) mysession.getAttribute("shopCart");
35. String action = request.getParameter("action");
36. if ("remove".equals(action)) {
37. String removeId = request.getParameter("removeId");
38. cart.removeProductFromCart(removeId);
39. } else if(action.equals("purchase")){
40. String[] productIds = request.getParameterValues("productId");
41.42. Map products = (Map) context.getAttribute("products");
43. if (cart == null) {
44. cart = new ShopCart();
45. mysession.setAttribute("shopCart", cart);
46. }
47. if (productIds == null) {
48. productIds = new String[0];
49. }
50. for (int i = 0; i < productIds.length; i++) {
51. Product product = (Product) products.get(productIds[i]);
52. cart.addProductToCart(product);
53. }
54. }
55. }catch(NullPointerException e)
56. {e.printStackTrace();}
57. %>
58.
59.60.
61.
ShopCartJSP.jsp:::::::::
1.
2.
3.
4.String path = request.getContextPath();
5.String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
6.%>
7.8.
9.
10.
11.
12.
13.
My JSP 'ShowCartJSP.jsp' starting page
14.
15.
16.
17.
18.
19.
20.
23.24.
25.
26.
27. HttpSession mysession = request.getSession();
28. ShopCart cart = (ShopCart) mysession.getAttribute("shopCart");
29. List products = null;
30. if (cart == null
31. || (products = cart.getAllProductsFromCart()) == null) {
32. %>
33.
34.
35. 你目前没有购买任何产品
36.
37.
38.
39. 返回产品显示页
40.
41.
42. } else {
43. Iterator iterator = products.iterator();
44. %>
45.
46.
47. 你目前购买的产品为:
48.
49.
50.
51.
52.
53. 产品名称
54.
55.
56. 产品描述
57.
58.
59. 价格
60.
61.
62. 操作
63.
64.
65.
66. while (iterator.hasNext()) {
67. Product productItem = (Product) iterator.next();
68. %>
69.
70.
71.
72.
73.
74.
75.
76.
77.78.
79.
80. href="/helloApp/shopCart/ShoppingJSP.jsp?action=remove&removeId=">删除
81.
82.
83.84.
85. }
86. %>
87.88.
89.
90. 目前您购物车的总价格为:
91. 元人民币。
92.
93.
94.
95. 返回产品显示页
96.
97.
98. }
99. %>
100.
101.
html简单购物车,用jsp实现一个简单的购物车web应用系统。相关推荐
- 利用cookie和jsp写一个简单的登录判断的网页,并获取上次的登录的时间。
要求:因为只是简单制作,没必要连数据库,直接用确定值去判断.然后就是第一次登录的时候需要获取到登录的时间,然后在下次登录的时候将上次获取到的时间输出. 思路:首先第一个,第一次登录的jsp(姑且当做注 ...
- php简单的购物车,利用PHP实现一个简单购物车的demo示例代码
利用PHP实现一个简单购物车的demo示例代码 数据结构跟关于PHP写购物车大体差不多,这里站长主要就购物车的主要业务逻辑进行一下说明: 1.用户未登陆时只能浏览商品,不能将其加入购物车 2.当未登陆 ...
- JSP写一个简单的登录界面
系列文章目录 JSP实现不链接数据库的简单登录功能实现 文章目录 系列文章目录 前言 一.jsp是什么? 二.使用到的页面代码 1.login.jsp 2.index.jsp 3.LoginServl ...
- python简单小游戏代码_一个简单的python小游戏---七彩同心圆
本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理 用pygame做一个简单的python小游戏-七彩同心圆 玩法:每次点击鼠标时,会以鼠标为圆心,不断 ...
- php简单的mysql类_一个简单的php mysql操作类
本文分享一个简单的php.mysql操作类,很简单,主要是数据的连接.查询等.有需要的朋友参考下吧. 分享一段php.mysql操作类的代码,供初学的朋友参考. 一个简单的类使用php和mysql数据 ...
- atm取款机的简单程序代码_LeNet:一个简单的卷积神经网络PyTorch实现
前两篇文章分别介绍了卷积层和池化层,卷积和池化是卷积神经网络必备的两大基础.本文我们将介绍一个早期用来识别手写数字图像的卷积神经网络:LeNet[1].LeNet名字来源于论文的第一作者Yann Le ...
- php编写一个简单计算器程序,PHP做一个简单的计算器
本文为大家讲解通过分支循环知识及PHP的表单处理一个简单的计算器 首先看下效果 通过网页显示计算器样式 在网页样式中可以看出,有两个输入框Num1.Num2和一个下拉框Type 还有一个提交按钮 在输 ...
- 用VB制作简单加法计算机,用VB设计一个简单的加法程序
计算机的多媒体技术已经广泛普及, 对于一般的用户来说, 采用可视化编程工具是的选择, VB程序设计语言就是其中一个典型的代表.Visual Basic是Microsoft Windows的编程语言.V ...
- 用JSP做一个简单的抽奖页面
首先写两个jsp文件 info.jsp <body><%HashMap<String, Integer> scoreMap = (HashMap<String, I ...
最新文章
- 深度学习模型压缩与加速综述!
- python 日历_python中的日历和时间
- muduo之TcpClient
- bitmapdata的知识点
- python语言的单行注释以井号开头_【学习】Python语言入门
- 【平面设计】扁平化设计(Ⅲ)——原则
- 关于dependency的scope
- Git学习系列之Debian或Ubuntu上安装Git详细步骤(图文详解)
- CentOS7.0下安装PHP5.6.30服务
- 蚂蚁课堂视频笔记思维导图-3期 三、性能优化专题
- 新浪2010南非足球世界杯漂亮表格的制作
- ps快捷图标在哪个文件夹_ps安装包在哪个文件夹
- 4个简单有效的网页视频下载方法,超级简单好用
- tensorflow 随笔-----------VGG网络的模型的复现
- 山东大学计算机组成与设计实验七 节拍脉冲发生器时序电路
- 门窗生产工厂MES系统,功能需求说明文档
- 头一回见!提升10倍效率,阿里给业务校验平台插上了AI的翅膀
- 芜湖c语言市赛答案,安徽省芜湖市2019-2020学年高二上学期期末考试 数学(文) Word版含答案...
- 一位IT新人对工作计划的心得体会
- 软件卸载不得不说的几个技巧
热门文章
- 大类资产配置(一)均值方差模型MOV
- 南卫理公会大学计算机科学,恭喜A同学获得南卫理公会大学计算机科学专业硕士通知书...
- 软件工程实践者的思想
- 什么是短网址?如何调用接口生成短地址?
- untiy发布webgl开发记录
- 丙腈PEG丙腈, CN-PEG-C
- 插入法排序c语言程序,C语言之插入排序算法
- Wifi密码破解与局域网抓包监听(小白--纯工具版)
- Intention Oriented Image Captions with Guiding Objects
- 一个可以截取其他App素材的办法Visual Studio Code