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

您所在的位置:网站首页 jsp商品购物代码及界面 用jsp实现一个简单的购物车web应用系统。实现的添加购物商品,删除购物商品并且显示购物车信息。

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

2024-07-09 14:58| 来源: 网络整理| 查看: 265

用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   :::::::::

                    My JSP 'ShowProductsJSP.jsp' starting page                                                                                    产品显示                查看购物车                                                                                                            序号                                                                产品名称                                                                产品描述                                                                产品价格(¥)                                                                添加到购物车                                                                                                                                                                                                                                                                                                                                                                                

                                            

            

ShoppingJSP.jsp::::::::::

                    My JSP 'shopping.jsp' starting page                                                                        My JSP 'ShowCartJSP.jsp' starting page                                                                  你目前没有购买任何产品                        

            返回产品显示页        

                                    你目前购买的产品为:                                                                        产品名称                                                    产品描述                                                    价格                                                    操作                                                                                                                                                                                                                删除                                                        

            目前您购物车的总价格为:            元人民币。        

        

                        返回产品显示页        

          

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

 

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3