用jsp实现一个简单的购物车web应用系统。 jsp实现简单聊天室
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.<%@ 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.10.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
11.<html>
12. <head>
13. <base href="<%=basePath%>">
14.15. <title>My JSP 'ShowProductsJSP.jsp' starting page</title>
16.17. <meta http-equiv="pragma" content="no-cache">
18. <meta http-equiv="cache-control" content="no-cache">
19. <meta http-equiv="expires" content="0">
20. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
21. <meta http-equiv="description" content="This is my page">
22. <!--
23. <link rel="stylesheet" type="text/css" href="styles.css">
24. -->
25.26. </head>
27.28. <body bgcolor="#ffffff">
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. <H1>
45. 产品显示
46. </H1>
47. <a href="/helloApp/shopCart/ShowCartJSP.jsp">查看购物车</a>
48.49. <form name="productForm" action="/helloApp/shopCart/ShoppingJSP.jsp" method="POST">
50. <input type="hidden" name="action" value="purchase">
51. <table border="1" cellspacing="0">
52. <tr bgcolor="#CCCCCC">
53. <tr bgcolor="#CCCCCC">
54. <td>
55. 序号
56. </td>
57. <td>
58. 产品名称
59. </td>
60. <td>
61. 产品描述
62. </td>
63. <td>
64. 产品价格(¥)
65. </td>
66. <td>
67. 添加到购物车
68. </td>
69. </tr>
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. %><tr>
79. <td>
80. <%=number++ %></td>
81. <td>
82. <%=product.getName()%>
83. </td>
84. <td><%=product.getDescription() %>
85. </td>
86. <td>
87. <%=product.getPrice() %></td>
88. <td>
89. <input type="checkbox" name="productId"
90. value="<%=product.getId() %>">
91. </td>
92. </tr>
93. <% }%>
94. </table>
95. <p>
96. <input type="reset" value="全部取消" />
97. <input type="submit" value="确定" />
98. </p>
99. </form>
100. </body>
101.</html>
102.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.10.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
11.<html>
12. <head>
13. <base href="<%=basePath%>">
14.15. <title>My JSP 'shopping.jsp' starting page</title>
16.17. <meta http-equiv="pragma" content="no-cache">
18. <meta http-equiv="cache-control" content="no-cache">
19. <meta http-equiv="expires" content="0">
20. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
21. <meta http-equiv="description" content="This is my page">
22. <!--
23. <link rel="stylesheet" type="text/css" href="styles.css">
24. -->
25.26. </head>
27.28. <body bgcolor="#ffffff">
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. <jsp:forward page="/shopCart/ShowCartJSP.jsp"></jsp:forward>
59.60. </body>
61.</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.8.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
9.<html>
10. <head>
11. <base href="<%=basePath%>">
12.
13. <title>My JSP 'ShowCartJSP.jsp' starting page</title>
14.
15. <meta http-equiv="pragma" content="no-cache">
16. <meta http-equiv="cache-control" content="no-cache">
17. <meta http-equiv="expires" content="0">
18. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
19. <meta http-equiv="description" content="This is my page">
20. <!--
21. <link rel="stylesheet" type="text/css" href="styles.css">
22. -->
23.24. </head>
25.
26. <body><%
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. <h1>
35. 你目前没有购买任何产品
36. </h1>
37.
38. <p>
39. <a href="/shopCart/ShowProductsJSP.jsp">返回产品显示页</a>
40. </p>
41. <%
42. } else {
43. Iterator iterator = products.iterator();
44. %>
45.
46. <h1>
47. 你目前购买的产品为:
48. </h1>
49.
50. <table border="1" cellspace="0">
51. <tr bgcolor="#CCCCCC">
52. <td>
53. 产品名称
54. </td>
55. <td>
56. 产品描述
57. </td>
58. <td>
59. 价格
60. </td>
61. <td>
62. 操作
63. </td>
64. </tr>
65. <%
66. while (iterator.hasNext()) {
67. Product productItem = (Product) iterator.next();
68. %>
69. <tr>
70. <td>
71. <%=productItem.getName()%>
72. </td>
73. <td><%=productItem.getDescription()%>
74. </td>
75. <td>
76. <%=productItem.getPrice()%></td>
77.78. <td>
79. <a
80. href="/helloApp/shopCart/ShoppingJSP.jsp?action=remove&removeId=<%=productItem.getId()%>">删除</a>
81. </td>
82. </tr>
83.84. <%
85. }
86. %>
87.88. </table>
89. <p>
90. 目前您购物车的总价格为:<%=cart.getAllProductPrice()%>
91. 元人民币。
92. </p>
93. <p>
94. </br>
95. <a href="/helloApp/shopCart/ShowProductsJSP.jsp">返回产品显示页</a>
96. </p>
97. <%
98. }
99. %>
100. </body>
101.</html>
最后打开Tomcat,在浏览器URL中输入http://localhost:8088/helloApp/shopCart/ShowProductsJSP.jsp
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yi76263725/archive/2008/09/14/2860224.aspx
更多阅读
如何让自己成为一个幸运的人? 我是一个幸运的人
如何让自己成为一个幸运的人?——简介 越是努力的人,就越幸运。如果想让自己成为一个幸运的人,就要努力。让自己在的身心都得到锻炼,通过努力让自己的身心变得更加好。让自己的生活富足,精神世界富足,心里欣慰。那么才能让自己的身心变得
一个用eXosip实现的UAC和UAS的例子调试成功 libexosip2
经过一段时间的学习,对sip总算有了一点认识,在学习过程中,遇到了太多的问题,郁闷过,惆怅过,但是一咬牙,还是过来了。令我感动的是,在网上遇到一些很热心的朋友,不厌其烦地给我以解惑,感谢他们,尤其是友善的大狗,呵呵,希望将来有一天他能看到这篇
用IBM F11打造一个自己的一键还原系统 f11一键还原官网
go [转贴]用IBM F11打造一个自己的一键还原系统1楼 发表于 2008-2-23 00:47 | 只看该作者 | 倒序看帖 | 打印无论本本或台式机都可以安装一个象品牌机一样的一键还原系统的,以下就简单介绍一下,要求读者要对分区和DOS有一点基础。
一个打工仔的两个一千万 打工仔挖地窖囚禁女孩
[致富经]榜样20强(4)一个打工仔的两个一千万(2010.10.14)发布时间:2010年10月14日 22:17农业书屋 收录于2011-04-06http://sannong.cntv.cn/program/zhifujing/20101014/104498.shtml2006年,四川省邛崃市为了发展经济,开始对外招商引
不必恐慌!毫西弗mSv ,用一个简单公式来估算福岛到本地的核辐 翠西亚西弗
日本福岛核电站的爆炸事故的核辐射引起了世人的关注,我们来用一个简单方法来计算一下到福岛当地核辐射达到多大强度才会影响本地的安全。我们知道圆形面积的半径增加一倍面积就会增加4倍,那么如果离开事故核电站一公里(KM)处核辐射达到