#截图:
JSP简单购物篮程序_java
JSP简单购物篮程序_bootstrap_02
JSP简单购物篮程序_ico_03

JSP简单购物篮程序_html_04

代码:

// Product.java
package entity;
public class Product {
	private Integer id;
	private String name;
	private Float price;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Float getPrice() {
		return price;
	}
	public void setPrice(Float price) {
		this.price = price;
	}
}

//ProductInCart.java
package entity;
public class ProductInCart extends Product {
	private Integer count;
	private Float total;
	public Integer getCount() {
		return count;
	}
	public void setCount(Integer count) {
		this.count = count;
	}
	public Float getTotal() {
		return total;
	}
	public void setTotal(Float total) {
		this.total = total;
	}	
}

//CartUtil.java
package util;
import java.util.ArrayList;
import java.util.List;

import entity.Product;
import entity.ProductInCart;
public class CartUtil{
	 private List<ProductInCart>ps=new ArrayList<ProductInCart>();// 得到购物车中的所有商品
	 public List<ProductInCart>Getlists(){
		return ps;
	}
	 public void addItem(int id) {
		 Product _p=ProductUtil.find(id);
		 if(_p!=null) {
			 if(ps.size()==0) {
				 ProductInCart newp=new ProductInCart();
				 newp.setId(_p.getId());
				 newp.setName(_p.getName());
				 newp.setPrice(_p.getPrice());
				 newp.setCount(1);
				 newp.setTotal(_p.getPrice());
				 ps.add(newp);
			 }
			 else {
				 int i=0;
				 for(;i<ps.size();i++) {
					 ProductInCart temp=(ProductInCart)ps.get(i);
					 if(temp.getName().equals(_p.getName())) {
						 temp.setCount(temp.getCount()+1);
						 temp.setTotal(_p.getPrice()*temp.getCount());
						 break;
					 }
				 }
				 if(i>=ps.size()) {
					 ProductInCart newp=new ProductInCart();
					 //Product _p=ProductUtil.find(id);
					 newp.setId(_p.getId());
					 newp.setName(_p.getName());
					 newp.setPrice(_p.getPrice());
					 newp.setCount(1);
					 newp.setTotal(_p.getPrice());
					 ps.add(newp);
				 }
			 }
		 } 
	 }   
	 // 根据页面传来的id参数将一件对应商品添加到购物车
	 public void delete(int id) {// 根据页面传来的id参数将一件对应商品从购物车中删除
		 for(int i=0;i<ps.size();i++) {
			 ProductInCart temp=(ProductInCart)ps.get(i);
			 if(temp.getId().equals(id)) {
				 if(temp.getCount()>1) {
				 temp.setCount(temp.getCount()-1);
				 temp.setTotal(temp.getPrice()*temp.getCount());
				 break; 
				 }
				 else if(temp.getCount()==1) {
					 ps.remove(i);
				 }
			 }
		 } 
	 }        
	public void clear() { // 清空购物车中的全部商品
		ps.clear();	
	}       
	public float calcTotalPrice() {// 计算购物车中全部商品的总价
		float totals=0f;
		for(int i=0;i<ps.size();i++) {
			ProductInCart temp=(ProductInCart)ps.get(i);
			totals+=temp.getTotal();
		}
		return totals;
	}      
}

//ProductUtil.java
package util;
import java.util.ArrayList;
import java.util.List;

import entity.Product;
public class ProductUtil {
	private static List<Product> products;//顺序存储
	private static Float[] prices= {1F,2F,3F};
	private static String[] names= {"手机","笔记本","手表"};
	static {
		products=new ArrayList<Product>();//声明成数组
		for(int i=0;i<names.length;i++) {
			Product p=new Product();
			p.setId(i+1);//设置id
			p.setName(names[i]);//设置商品名称
			p.setPrice(prices[i]);//设置商品价格
			products.add(p);
		}
	}
	public List<Product> getAll(){
		return products;
	}
	public static Product find(int id) {
		for(Product p:products) {
			if(p.getId()==id) {//如果找到,则返回此行数组
				return p;
			}
		}
		return null;
	}
}

//cart.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ page import="entity.ProductInCart"%>
<%@ page import="entity.Product"%>
<%@ page import="util.ProductUtil"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head >
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" >
 <!-- 4不支持默认图标 要用3 -->
	<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">  
	<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
	<script src="https://cdn.staticfile.org/popper.js/1.12.5/umd/popper.min.js"></script>
  </head>
  

  <jsp:useBean id="CartUtil" class="util.CartUtil" scope="session"/>
  <%
  	String action=request.getParameter("action");
  	if("add".equals(action)){
  		String id=request.getParameter("id");
  		CartUtil.addItem(Integer.parseInt(id));	
  	}
  	else if("del".equals(action)){
  		String id=request.getParameter("id");
  		CartUtil.delete(Integer.parseInt(id));
  	}
  	else if("clear".equals(action)){
  		
  		CartUtil.clear();
  	}
	List<ProductInCart> productsincart=CartUtil.Getlists(); 
	if(productsincart==null||productsincart.size()==0){
		out.write("<button type='button' class='btn btn-outline-danger'>购物车为空!!!</button>");	
	} else{	 
  	 %>
  <body>
<table class="table">
<div class="col-sm-1">
	       	<a type="button" class="btn btn-danger" href="${pageContext.request.contextPath }/product.jsp">
				<span class="glyphicon glyphicon-arrow-left" >继续购物</span>
			</a>
       	</div>
  <thead>
   <tr class="table-primary">
      <th scope="col">id</th>
      <th scope="col">商品名称</th>
      <th scope="col">单价</th>
      <th scope="col">数量</th>
   	  <th scope="col">小计</th>
   	  <th scope="col">删除</th>
    </tr>
  </thead>
 <tbody>
<% for(ProductInCart pic:productsincart){
  		out.write("<tr class='table-success'>");
  		out.write("<td>"+pic.getId()+"</td>");
  		out.write("<td>"+pic.getName()+"</td>");
  		out.write("<td>"+pic.getPrice()+"</td>");
  		out.write("<td>"+pic.getCount()+"</td>");
  		out.write("<td>"+pic.getTotal()+"</td>");
  		%>
  		<td><a href="cart.jsp?action=del&id=<%= pic.getId()%>"><span class="glyphicon glyphicon-minus-sign"></span></a></td>
  		<% 
  		out.write("</tr>");
  		}%> 
     <tr><td ></td><td></td><td></td><td></td>
     	<td ><font style="font-size:30px;color:red">总计:</font><%=CartUtil.calcTotalPrice()%></td>
     	<td><a href="cart.jsp?action=clear"><button type="button" class="btn btn-outline-danger">清空购物车</button></a></td>
     </tr>		
  	<% }%> 
  </tbody>
	</table>
  </body>
</html>
//product.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="entity.Product"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head >
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" >
	<!-- 4不支持默认图标 要用3 -->
	<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">  
	<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
	<script src="https://cdn.staticfile.org/popper.js/1.12.5/umd/popper.min.js"></script>
  </head>

  <jsp:useBean id="ProductUtil" class="util.ProductUtil" scope="application"/>
  <body>
<div >
	<a href="cart.jsp">
		<button type="button" class="btn btn-outline-danger">
			<span class="glyphicon glyphicon-shopping-cart">查看购物车</span>
		</button>
	</a>
</div>
<table class="table">
  <thead>
    <tr class="table-primary">
      <th scope="col">id</th>
      <th scope="col">商品名称</th>
      <th scope="col">单价</th>
   	  <th scope="col">加入购物车</th>
    </tr>
  </thead>
  <tbody>
 <% List<Product> products=ProductUtil.getAll();
  	for(Product p:products){
  		out.write("<tr class='table-success'>");
  		out.write("<td>"+p.getId()+"</td>");
  		out.write("<td>"+p.getName()+"</td>");
  		out.write("<td>"+p.getPrice()+"</td>");
  		%>
  		<td>
  			<a href="cart.jsp?action=add&id=<%= p.getId()%>">
  				<span class="glyphicon glyphicon-shopping-cart"></span>
  			</a>
  		</td>
  		<% 
  		out.write("</tr>");
  	}
  	%> 
 </tbody>
</table>
  </body>
 </html>