用jsp实现一个简单的购物车web应用系统。实现的添加购物商品,删除购物商品并且显示购物车信息。

时间:2021-06-07 20:50:18

jsp实现一个简单的购物车web应用系统。实现的添加购物商品,删除购物商品并且显示购物车信息。

 1. 在自己建立的WEB工程中,建立包shopcart.dto,在相应的包中添加类Product.java ,ShopCart.java

  1. /*类Product */
  2. package shopcart.dto;
  3. import java.io.Serializable;
  4. public class Product implements Serializable
  5. {
  6.     private String id;//产品标识
  7.     private String name;//产品名称
  8.     private String description;//产品描述
  9.     private double price;//产品价格
  10.     public Product()
  11.     {
  12.     }
  13.     public Product(String id, String name, String description, double price)
  14.     {
  15.         this.id = id;
  16.         this.name = name;
  17.         this.description = description;
  18.         this.price = price;
  19.     }
  20.     public void setId(String id)
  21.     {
  22.         this.id = id;
  23.     }
  24.     public void setName(String name)
  25.     {
  26.         this.name = name;
  27.     }
  28.     public void setDescription(String description)
  29.     {
  30.         this.description = description;
  31.     }
  32.     public void setPrice(double price)
  33.     {
  34.         this.price = price;
  35.     }
  36.     public String getId()
  37.     {
  38.         return id;
  39.     }
  40.     public String getName()
  41.     {
  42.         return name;
  43.     }
  44.     public String getDescription()
  45.     {
  46.         return description;
  47.     }
  48.     public double getPrice()
  49.     {
  50.         return price;
  51.     }
  52. }
  53. /*类ShopCart */
  54. package shopcart.dto;
  55. import java.io.Serializable;
  56. import java.util.*;
  57. public class ShopCart implements Serializable
  58. {
  59.     public ShopCart()
  60.     {
  61.     }
  62.     private List cart = null;
  63.     /**
  64.      * 添加一个产品到购物车
  65.      * @param product Product
  66.      */
  67.     public void addProductToCart(Product product)
  68.     {
  69.         if (cart == null)
  70.             cart = new ArrayList();
  71.         Iterator it = cart.iterator();
  72.         while (it.hasNext())
  73.         {
  74.             Product item = (Product) it.next();
  75.             if (item.getId().equals(product.getId()))
  76.             {
  77.                 return;
  78.             }
  79.         }
  80.         cart.add(product);
  81.     }
  82.     /**
  83.      * 从购物车中删除一产品
  84.      * @param productId String 产品id
  85.      */
  86.     public void removeProductFromCart(String productId)
  87.     {
  88.         if (cart == null)
  89.             return;
  90.         Iterator it = cart.iterator();
  91.         while (it.hasNext())
  92.         {
  93.             Product item = (Product) it.next();
  94.             if (item.getId().equals(productId))
  95.             {
  96.                 it.remove();
  97.                 return;
  98.             }
  99.         }
  100.     }
  101.     /**
  102.      * 计算购物车中的商品价格
  103.      * @return double 商品价格总数
  104.      */
  105.     public double getAllProductPrice()
  106.     {
  107.         if (cart == null)
  108.             return 0;
  109.         double totalPrice = 0;
  110.         Iterator it = cart.iterator();
  111.         while (it.hasNext())
  112.         {
  113.             Product item = (Product) it.next();
  114.             totalPrice += item.getPrice();
  115.         }
  116.         return totalPrice;
  117.     }
  118.     /**
  119.      * 返回购物车所有产品信息
  120.      * @return List
  121.      */
  122.     public List getAllProductsFromCart()
  123.     {
  124.         return cart;
  125.     }
  126. }

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. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  10. <html>
  11.     <head>
  12.         <base href="<%=basePath%>">
  13.         <title>My JSP 'ShowProductsJSP.jsp' starting page</title>
  14.         <meta http-equiv="pragma" content="no-cache">
  15.         <meta http-equiv="cache-control" content="no-cache">
  16.         <meta http-equiv="expires" content="0">
  17.         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  18.         <meta http-equiv="description" content="This is my page">
  19.         <!--
  20.     <link rel="stylesheet" type="text/css" href="styles.css">
  21.     -->
  22.     </head>
  23.     <body bgcolor="#ffffff">
  24.         <%
  25.             Map products = new HashMap();
  26.             products.put("001"new Product("001""mp3播放器",
  27.                     "效果很不错的mp3播放器,存储空间达1GB", 999.00));
  28.             products.put("002"new Product("002""数码相机""象素500万,10倍光学变焦",
  29.                     2500.00));
  30.             products.put("003"new Product("003""数码摄像机",
  31.                     "120万象素,支持夜景拍摄,20倍光学变焦", 5999.00));
  32.             products.put("004"new Product("004""迷你mp4",
  33.                     "市面所能见到的最好的mp4播放器,国产", 1999.99));
  34.             products.put("005"new Product("005""多功能手机",
  35.                     "集mp3播放、100万象素数码相机,手机功能于一体", 2199.99));
  36.             ServletContext context = getServletContext();
  37.         context.setAttribute("products", products);
  38.         %>
  39.         <H1>
  40.             产品显示
  41.         </H1>
  42.         <a href="/helloApp/shopCart/ShowCartJSP.jsp">查看购物车</a>
  43.         <form name="productForm" action="/helloApp/shopCart/ShoppingJSP.jsp" method="POST">
  44.             <input type="hidden" name="action" value="purchase">
  45.             <table border="1" cellspacing="0">
  46.                 <tr bgcolor="#CCCCCC">
  47.                 <tr bgcolor="#CCCCCC">
  48.                     <td>
  49.                         序号
  50.                     </td>
  51.                     <td>
  52.                         产品名称
  53.                     </td>
  54.                     <td>
  55.                         产品描述
  56.                     </td>
  57.                     <td>
  58.                         产品价格(¥)
  59.                     </td>
  60.                     <td>
  61.                         添加到购物车
  62.                     </td>
  63.                 </tr>
  64.                 <%
  65.                     Set productIdSet = products.keySet();
  66.                     Iterator it = productIdSet.iterator();
  67.                     int number = 1;
  68.             
  69.                     while (it.hasNext()) {
  70.                         String id = (String) it.next();
  71.                         Product product = (Product) products.get(id);
  72.                         %><tr>
  73.                     <td>
  74.                         <%=number++ %></td>
  75.                     <td>
  76.                         <%=product.getName()%>
  77.                     </td>
  78.                     <td><%=product.getDescription() %>
  79.                     </td>
  80.                     <td>
  81.                         <%=product.getPrice() %></td>
  82.                     <td>
  83.                         <input type="checkbox" name="productId"
  84.                             value="<%=product.getId() %>">
  85.                     </td>
  86.                 </tr>
  87.                     <% }%>
  88.             </table>
  89.             <p>
  90.                 <input type="reset" value="全部取消" />
  91.                 <input type="submit" value="确定" />
  92.             </p>
  93.         </form>
  94.     </body>
  95. </html>

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. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  10. <html>
  11.     <head>
  12.         <base href="<%=basePath%>">
  13.         <title>My JSP 'shopping.jsp' starting page</title>
  14.         <meta http-equiv="pragma" content="no-cache">
  15.         <meta http-equiv="cache-control" content="no-cache">
  16.         <meta http-equiv="expires" content="0">
  17.         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  18.         <meta http-equiv="description" content="This is my page">
  19.         <!--
  20.     <link rel="stylesheet" type="text/css" href="styles.css">
  21.     -->
  22.     </head>
  23.     <body bgcolor="#ffffff">
  24.         <%
  25.             try{
  26.             response.setContentType("text/html; charset=GBK");
  27.             HttpSession mysession = request.getSession();
  28.             ServletContext context = getServletContext();
  29.             ShopCart cart = (ShopCart) mysession.getAttribute("shopCart");
  30.             String action = request.getParameter("action");
  31.             if ("remove".equals(action)) {
  32.                 String removeId = request.getParameter("removeId");
  33.                 cart.removeProductFromCart(removeId);
  34.             } else if(action.equals("purchase")){
  35.                 String[] productIds = request.getParameterValues("productId");
  36.                 Map products = (Map) context.getAttribute("products");
  37.                 if (cart == null) {
  38.                     cart = new ShopCart();
  39.                     mysession.setAttribute("shopCart", cart);
  40.                 }
  41.                 if (productIds == null) {
  42.                     productIds = new String[0];
  43.                 }
  44.                 for (int i = 0; i < productIds.length; i++) {
  45.                     Product product = (Product) products.get(productIds[i]);
  46.                     cart.addProductToCart(product);
  47.                 }
  48.             }
  49.             }catch(NullPointerException e)
  50.             {e.printStackTrace();}
  51.         %>
  52.         <jsp:forward page="/shopCart/ShowCartJSP.jsp"></jsp:forward>
  53.     </body>
  54. </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. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  8. <html>
  9.   <head>
  10.     <base href="<%=basePath%>">
  11.     
  12.     <title>My JSP 'ShowCartJSP.jsp' starting page</title>
  13.     
  14.     <meta http-equiv="pragma" content="no-cache">
  15.     <meta http-equiv="cache-control" content="no-cache">
  16.     <meta http-equiv="expires" content="0">    
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  18.     <meta http-equiv="description" content="This is my page">
  19.     <!--
  20.     <link rel="stylesheet" type="text/css" href="styles.css">
  21.     -->
  22.   </head>
  23.   
  24.   <body><% 
  25.   HttpSession mysession = request.getSession();
  26.         ShopCart cart = (ShopCart) mysession.getAttribute("shopCart");
  27.     List products = null;
  28.             if (cart == null
  29.                     || (products = cart.getAllProductsFromCart()) == null) {
  30.         %>
  31.         
  32.         <h1>
  33.             你目前没有购买任何产品
  34.         </h1>
  35.         
  36.         <p>
  37.             <a href="/shopCart/ShowProductsJSP.jsp">返回产品显示页</a>
  38.         </p>
  39.         <%
  40.             } else {
  41.                 Iterator iterator = products.iterator();
  42.         %>
  43.         
  44.         <h1>
  45.             你目前购买的产品为:
  46.         </h1>
  47.         
  48.         <table border="1" cellspace="0">
  49.             <tr bgcolor="#CCCCCC">
  50.                 <td>
  51.                     产品名称
  52.                 </td>
  53.                 <td>
  54.                     产品描述
  55.                 </td>
  56.                 <td>
  57.                     价格
  58.                 </td>
  59.                 <td>
  60.                     操作
  61.                 </td>
  62.             </tr>
  63.             <%
  64.                 while (iterator.hasNext()) {
  65.                         Product productItem = (Product) iterator.next();
  66.             %>
  67.             <tr>
  68.                 <td>
  69.                     <%=productItem.getName()%>
  70.                 </td>
  71.                 <td><%=productItem.getDescription()%>
  72.                 </td>
  73.                 <td>
  74.                     <%=productItem.getPrice()%></td>
  75.                 <td>
  76.                     <a
  77.                         href="/helloApp/shopCart/ShoppingJSP.jsp?action=remove&removeId=<%=productItem.getId()%>">删除</a>
  78.                 </td>
  79.             </tr>
  80.             <%
  81.                 }
  82.             %>
  83.         </table>
  84.         <p>
  85.             目前您购物车的总价格为:<%=cart.getAllProductPrice()%>
  86.             元人民币。
  87.         </p>
  88.         <p>
  89.             </br>
  90.             <a href="/helloApp/shopCart/ShowProductsJSP.jsp">返回产品显示页</a>
  91.         </p>
  92.         <%
  93.             }
  94.         %>
  95.   </body>
  96. </html>

3.最后打开Tomcat,在浏览器URL中输入http://localhost:8088/helloApp/shopCart/ShowProductsJSP.jsp即可!

<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /> <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 用jsp实现一个简单的购物车web应用系统。实现的添加购物商品,删除购物商品并且显示购物车信息。

用jsp实现一个简单的购物车web应用系统。实现的添加购物商品,删除购物商品并且显示购物车信息。

用jsp实现一个简单的购物车web应用系统。实现的添加购物商品,删除购物商品并且显示购物车信息。