用jsp实现一个简单的购物车web应用系统。实现的添加购物商品,删除购物商品并且显示购物车信息。
1. 在自己建立的WEB工程中,建立包shopcart.dto,在相应的包中添加类Product.java ,ShopCart.java
- /*类Product */
- package shopcart.dto;
- import java.io.Serializable;
- public class Product implements Serializable
- {
- private String id;//产品标识
- private String name;//产品名称
- private String description;//产品描述
- private double price;//产品价格
- public Product()
- {
- }
- public Product(String id, String name, String description, double price)
- {
- this.id = id;
- this.name = name;
- this.description = description;
- this.price = price;
- }
- public void setId(String id)
- {
- this.id = id;
- }
- public void setName(String name)
- {
- this.name = name;
- }
- public void setDescription(String description)
- {
- this.description = description;
- }
- public void setPrice(double price)
- {
- this.price = price;
- }
- public String getId()
- {
- return id;
- }
- public String getName()
- {
- return name;
- }
- public String getDescription()
- {
- return description;
- }
- public double getPrice()
- {
- return price;
- }
- }
- /*类ShopCart */
- package shopcart.dto;
- import java.io.Serializable;
- import java.util.*;
- public class ShopCart implements Serializable
- {
- public ShopCart()
- {
- }
- private List cart = null;
- /**
- * 添加一个产品到购物车
- * @param product Product
- */
- public void addProductToCart(Product product)
- {
- if (cart == null)
- cart = new ArrayList();
- Iterator it = cart.iterator();
- while (it.hasNext())
- {
- Product item = (Product) it.next();
- if (item.getId().equals(product.getId()))
- {
- return;
- }
- }
- cart.add(product);
- }
- /**
- * 从购物车中删除一产品
- * @param productId String 产品id
- */
- public void removeProductFromCart(String productId)
- {
- if (cart == null)
- return;
- Iterator it = cart.iterator();
- while (it.hasNext())
- {
- Product item = (Product) it.next();
- if (item.getId().equals(productId))
- {
- it.remove();
- return;
- }
- }
- }
- /**
- * 计算购物车中的商品价格
- * @return double 商品价格总数
- */
- public double getAllProductPrice()
- {
- if (cart == null)
- return 0;
- double totalPrice = 0;
- Iterator it = cart.iterator();
- while (it.hasNext())
- {
- Product item = (Product) it.next();
- totalPrice += item.getPrice();
- }
- return totalPrice;
- }
- /**
- * 返回购物车所有产品信息
- * @return List
- */
- public List getAllProductsFromCart()
- {
- return cart;
- }
- }
2.在WebRoot目录下添加包shopCart 在里边添加ShowProductsJSP.jsp ShoppingJSP.jsp ShopCartJSP.jsp
ShowProductsJSP.jsp :::::::::
- <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
- <%@ page import="shopcart.dto.*"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme() + "://"
- + request.getServerName() + ":" + request.getServerPort()
- + path + "/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'ShowProductsJSP.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
- <body bgcolor="#ffffff">
- <%
- Map products = new HashMap();
- products.put("001", new Product("001", "mp3播放器",
- "效果很不错的mp3播放器,存储空间达1GB", 999.00));
- products.put("002", new Product("002", "数码相机", "象素500万,10倍光学变焦",
- 2500.00));
- products.put("003", new Product("003", "数码摄像机",
- "120万象素,支持夜景拍摄,20倍光学变焦", 5999.00));
- products.put("004", new Product("004", "迷你mp4",
- "市面所能见到的最好的mp4播放器,国产", 1999.99));
- products.put("005", new Product("005", "多功能手机",
- "集mp3播放、100万象素数码相机,手机功能于一体", 2199.99));
- ServletContext context = getServletContext();
- context.setAttribute("products", products);
- %>
- <H1>
- 产品显示
- </H1>
- <a href="/helloApp/shopCart/ShowCartJSP.jsp">查看购物车</a>
- <form name="productForm" action="/helloApp/shopCart/ShoppingJSP.jsp" method="POST">
- <input type="hidden" name="action" value="purchase">
- <table border="1" cellspacing="0">
- <tr bgcolor="#CCCCCC">
- <tr bgcolor="#CCCCCC">
- <td>
- 序号
- </td>
- <td>
- 产品名称
- </td>
- <td>
- 产品描述
- </td>
- <td>
- 产品价格(¥)
- </td>
- <td>
- 添加到购物车
- </td>
- </tr>
- <%
- Set productIdSet = products.keySet();
- Iterator it = productIdSet.iterator();
- int number = 1;
- while (it.hasNext()) {
- String id = (String) it.next();
- Product product = (Product) products.get(id);
- %><tr>
- <td>
- <%=number++ %></td>
- <td>
- <%=product.getName()%>
- </td>
- <td><%=product.getDescription() %>
- </td>
- <td>
- <%=product.getPrice() %></td>
- <td>
- <input type="checkbox" name="productId"
- value="<%=product.getId() %>">
- </td>
- </tr>
- <% }%>
- </table>
- <p>
- <input type="reset" value="全部取消" />
- <input type="submit" value="确定" />
- </p>
- </form>
- </body>
- </html>
ShoppingJSP.jsp::::::::::
- <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
- <%@ page import="shopcart.dto.*"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme() + "://"
- + request.getServerName() + ":" + request.getServerPort()
- + path + "/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'shopping.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
- <body bgcolor="#ffffff">
- <%
- try{
- response.setContentType("text/html; charset=GBK");
- HttpSession mysession = request.getSession();
- ServletContext context = getServletContext();
- ShopCart cart = (ShopCart) mysession.getAttribute("shopCart");
- String action = request.getParameter("action");
- if ("remove".equals(action)) {
- String removeId = request.getParameter("removeId");
- cart.removeProductFromCart(removeId);
- } else if(action.equals("purchase")){
- String[] productIds = request.getParameterValues("productId");
- Map products = (Map) context.getAttribute("products");
- if (cart == null) {
- cart = new ShopCart();
- mysession.setAttribute("shopCart", cart);
- }
- if (productIds == null) {
- productIds = new String[0];
- }
- for (int i = 0; i < productIds.length; i++) {
- Product product = (Product) products.get(productIds[i]);
- cart.addProductToCart(product);
- }
- }
- }catch(NullPointerException e)
- {e.printStackTrace();}
- %>
- <jsp:forward page="/shopCart/ShowCartJSP.jsp"></jsp:forward>
- </body>
- </html>
ShopCartJSP.jsp:::::::::
- <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
- <%@ page import="shopcart.dto.*" %>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'ShowCartJSP.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
- <body><%
- HttpSession mysession = request.getSession();
- ShopCart cart = (ShopCart) mysession.getAttribute("shopCart");
- List products = null;
- if (cart == null
- || (products = cart.getAllProductsFromCart()) == null) {
- %>
- <h1>
- 你目前没有购买任何产品
- </h1>
- <p>
- <a href="/shopCart/ShowProductsJSP.jsp">返回产品显示页</a>
- </p>
- <%
- } else {
- Iterator iterator = products.iterator();
- %>
- <h1>
- 你目前购买的产品为:
- </h1>
- <table border="1" cellspace="0">
- <tr bgcolor="#CCCCCC">
- <td>
- 产品名称
- </td>
- <td>
- 产品描述
- </td>
- <td>
- 价格
- </td>
- <td>
- 操作
- </td>
- </tr>
- <%
- while (iterator.hasNext()) {
- Product productItem = (Product) iterator.next();
- %>
- <tr>
- <td>
- <%=productItem.getName()%>
- </td>
- <td><%=productItem.getDescription()%>
- </td>
- <td>
- <%=productItem.getPrice()%></td>
- <td>
- <a
- href="/helloApp/shopCart/ShoppingJSP.jsp?action=remove&removeId=<%=productItem.getId()%>">删除</a>
- </td>
- </tr>
- <%
- }
- %>
- </table>
- <p>
- 目前您购物车的总价格为:<%=cart.getAllProductPrice()%>
- 元人民币。
- </p>
- <p>
- </br>
- <a href="/helloApp/shopCart/ShowProductsJSP.jsp">返回产品显示页</a>
- </p>
- <%
- }
- %>
- </body>
- </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" />