1. 在自己建立的WEB工程中,建立包shopcart.dto,在相應(yīng)的包中添加類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;//產(chǎn)品標(biāo)識 10. private String name;//產(chǎn)品名稱 11. private String description;//產(chǎn)品描述 12. private double price;//產(chǎn)品價格 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. * 添加一個產(chǎn)品到購物車 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. * 從購物車中刪除一產(chǎn)品 103. * @param productId String 產(chǎn)品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 商品價格總數(shù) 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. * 返回購物車所有產(chǎn)品信息 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播放器,存儲空間達(dá)1GB", 999.00)); 33. products.put("002", new Product("002", "數(shù)碼相機", "象素500萬,10倍光學(xué)變焦", 34. 2500.00)); 35. products.put("003", new Product("003", "數(shù)碼攝像機", 36. "120萬象素,支持夜景拍攝,20倍光學(xué)變焦", 5999.00)); 37. products.put("004", new Product("004", "迷你mp4", 38. "市面所能見到的最好的mp4播放器,國產(chǎn)", 1999.99)); 39. products.put("005", new Product("005", "多功能手機", 40. "集mp3播放、100萬象素數(shù)碼相機,手機功能于一體", 2199.99)); 41. ServletContext context = getServletContext(); 42. context.setAttribute("products", products); 43. %> 44. <H1> 45. 產(chǎn)品顯示 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. 產(chǎn)品名稱 59. </td> 60. <td> 61. 產(chǎn)品描述 62. </td> 63. <td> 64. 產(chǎn)品價格(¥) 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. 你目前沒有購買任何產(chǎn)品 36. </h1> 37. 38. <p> 39. <a href="/shopCart/ShowProductsJSP.jsp">返回產(chǎn)品顯示頁</a> 40. </p> 41. <% 42. } else { 43. Iterator iterator = products.iterator(); 44. %> 45. 46. <h1> 47. 你目前購買的產(chǎn)品為: 48. </h1> 49. 50. <table border="1" cellspace="0"> 51. <tr bgcolor="#CCCCCC"> 52. <td> 53. 產(chǎn)品名稱 54. </td> 55. <td> 56. 產(chǎn)品描述 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">返回產(chǎn)品顯示頁</a> 96. </p> 97. <% 98. } 99. %> 100. </body> 101.</html> 最后打開Tomcat,在瀏覽器URL中輸入http://localhost:8088/helloApp/shopCart/ShowProductsJSP.jsp
本文來自CSDN博客,轉(zhuǎn)載請標(biāo)明出處:http://blog.csdn.net/yi76263725/archive/2008/09/14/2860224.aspx
|
|