JSP+javabean实现购物车功能

简单的小程序,java后台 + Web前端,可以实现购物车的添加,删除等功能,并没有用到数据库。而是用到的session存取功能。

                                                                          

Product.java

package shopping.cart;


import java.io.Serializable;


@SuppressWarnings("serial")
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 this.id;
	}
	public String getName(){
		return this.name;
	}
	public String getDescription(){
		return this.description;
	}
	public double getPrice(){
		return this.price;
	}
}

CartItem.java
package shopping.cart;

public class CartItem {  
    private Product product;  
  
    private int count;  
  
    public int getCount() {  
        return count;  
    }  
  
    public void setCount(int count) {  
        this.count = count;  
    }  
  
    public Product getProduct() {  
        return product;  
    }  
  
    public void setProduct(Product product) {  
        this.product = product;  
    }  
}
Cart.java
package shopping.cart;


import java.util.ArrayList;  
import java.util.Iterator;  
import java.util.List;  
  
public class Cart {  
    List items = new ArrayList();  
  
    public List getItems() {  
        return items;  
    }  
  
    public void setItems(List items) {  
        this.items = items;  
    }  
      
    public void add(CartItem ci) {  
        for (Iterator iter = items.iterator(); iter.hasNext();) {  
            CartItem item = iter.next();  
            if(item.getProduct().getId() == ci.getProduct().getId()) {  
                item.setCount(item.getCount() + 1);  
                return;  
            }  
        }  
          
        items.add(ci);  
    }  
      
    public double getTotalPrice() {  
        double d = 0.0;  
        for(Iterator it = items.iterator(); it.hasNext(); ) {  
            CartItem current = it.next();  
            d += current.getProduct().getPrice() * current.getCount();  
        }  
        return d;  
    }  
      
    public void deleteItemById(String productId) {  
        for (Iterator iter = items.iterator(); iter.hasNext();) {  
            CartItem item = iter.next();  
            if(item.getProduct().getId().equals(productId)) {
                iter.remove();
                return;
            }
        }
    }  
}
ShowProducts.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>  
<%@ page import="shopping.cart.*"%>  
<%
    String path = request.getContextPath();  
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";  
%>

  
  
    
        My JSP 'ShowProductsJSP.jsp' starting page  
          
          
          
          
           
      
      
        <% 
            Map products = new HashMap();  
            products.put("1612001", new Product("1612001", "mp3播放器","效果很不错的mp3播放器,存储空间达1GB", 100.00));  
            products.put("1612002", new Product("1612002", "数码相机", "像素500万,10倍光学变焦",500.00));  
            products.put("1612003", new Product("1612003", "数码摄像机","120万象素,支持夜景拍摄,20倍光学变焦", 200.00));  
            products.put("1612004", new Product("1612004", "迷你mp4","市面所能见到的最好的mp4播放器,国产", 300.00));  
            products.put("1612005", new Product("1612005", "多功能手机","集mp3播放、100万象素数码相机,手机功能于一体", 400.00));  
            products.put("1612006", new Product("1612006", "苹果4S","高贵的品牌手机,智能手机,公鸡中的战斗机",2399.00)); 
            session.setAttribute("products", products);
        %>  
        

产品显示

<% 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); %> <% } %>
序号 产品名称 产品描述 产品单价(¥) 添加到购物车
<%=number++%> <%=product.getName()%> <%=product.getDescription()%> <%=product.getPrice()%> 我要购买

Buy.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>  
<%@ page import="shopping.cart.*" %>
<%  
    Cart c = (Cart)session.getAttribute("cart");  
    if(c == null) {  
        c = new Cart();  
        session.setAttribute("cart", c);  
    }
    double totalPrice = c.getTotalPrice();
    request.setCharacterEncoding("GBK");  
    String action = request.getParameter("action");  
  
    Map products = (HashMap)session.getAttribute("products");  
  
    if(action != null && action.trim().equals("add")) {  
        String id = request.getParameter("id");  
        Product p = (Product)products.get(id);  
        CartItem ci = new CartItem();  
        ci.setProduct(p);  
        ci.setCount(1);  
        c.add(ci);  
    }  
  
    if(action != null && action.trim().equals("delete")) {  
        String id = request.getParameter("id");  
        c.deleteItemById(id);  
    }  
  
    if(action != null && action.trim().equals("update")) {  
        for(int i=0; i   
 
<%  
    String path = request.getContextPath();  
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
%>  

<%  
    List items = c.getItems();  
%>  
  
  
  
      
      
        购物车
      
    
          
        
<% for(Iterator it = items.iterator(); it.hasNext(); ) { CartItem ci = it.next(); %> <% } %>
产品ID 产品名称 购买数量 单价 总价 处理
<%=ci.getProduct().getId() %> <%=ci.getProduct().getName() %> " value="<%=ci.getCount() %>" οnkeypress="if (event.keyCode < 45 || event.keyCode > 57) event.returnValue = false;" οnchange="document.forms[0].submit()"> <%=ci.getProduct().getPrice() %> <%=ci.getProduct().getPrice()*ci.getCount() %> 删除
所有商品总价格为: <%=c.getTotalPrice() %>
下单


下面是采用同样session方法,但浅显易懂的一种方式:

地址:http://download.csdn.net/detail/qq_28306215/9702155

你可能感兴趣的:(JSP)