分析

  1. 图书初始化(图书属性类,图书工具类)
  2. 图书展示页面(showBook Servlet)
  3. 图书添加购物车(addCart Servlet)
  4. 图书购物车展示(showCart Servlet)
  5. 图书购物车清空(cleanCart Servlet)

实现

  • 图书展示

  • 查看购物车

  • 清空购物车

  • 提示:购物车没有商品

  • 提示:添加购物车成功

代码

图书属性类

 package bean;
 public class book {
/*
 * 
 * 图书类
 * 
 */
private String id;
private String name;
private double price;
private String author;
//图书类无参构造方法
public book() {
}
//图书类有参构造方法
public book(String id, String name, double price, String author) {
	super();
	this.id = id;
	this.name = name;
	this.price = price;
	this.author = author;
}
////图书类getter/setter方法
public String getId() {
	return id;
}
public void setId(String id) {
	this.id = id;
}
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public double getPrice() {
	return price;
}
public void setPrice(double price) {
	this.price = price;
}
public String getAuthor() {
	return author;
}
public void setAuthor(String author) {
	this.author = author;
}
//图书类toString方法
@Override
public String toString() {
	return "book [id=" + id + ", name=" + name + ", price=" + price + ", author=" + author + "]";
}
}

图书工具类

package util;
import java.util.HashMap;
import java.util.Map;
import bean.book;
public class DButil {
/*
 * 
 * 图书初始化
 * 
 */
private static Map<String,book> books=new HashMap<String,book>();
static {
	books.put("1", new book("1","Java基础12天",40,"马云"));
	books.put("2", new book("2","Java进阶15天",50,"雷军"));
	books.put("3", new book("3","Java网页16天",60,"刘虎"));
	books.put("4", new book("4","Java安卓17天",70,"江海"));
}
//显示所有图书
public static Map<String,book> findAllBooks(){
	return books;
}
//通过传进来id来找图书
public static book findBookById(String id){
	return books.get(id);
}
}

图书展示台

package shoppingCart;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import bean.book;
import util.DButil;
public class showBook extends HttpServlet {
private static final long serialVersionUID = 1L;
/*
 * 
 * 图书展示页面
 * 
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
	//设置字符编码
	response.setContentType("text/html;charset=utf-8");
	request.setCharacterEncoding("utf-8");
	//拿到向浏览器的write对象
	PrintWriter out = response.getWriter();
	out.write("=============全部经典书籍============<br><br>");
	//拿到全部图书并遍历显示,每行图书加个超链接到“加入购物车”servlet类并传个参数“ID”
	Map<String, book> books = DButil.findAllBooks();
	for (Map.Entry<String, book> book : books.entrySet()) {
		String url1 = request.getContextPath() + "/addCart?id=" + book.getKey();
		out.write("<a href='" + url1 + "'>" + "书名:《 " + book.getValue().getName() + "》&nbsp;&nbsp;&nbsp;作者: "
				+ book.getValue().getAuthor() + "&nbsp;&nbsp;&nbsp;价格: " + book.getValue().getPrice()
				+ "元 </a><br><br>");
	}
	//查看购物车
	String url2 = request.getContextPath() + "/showCart";
	out.write("<a href='" + url2 + "'>查看购物车</a><br><br>");
	//清空购物车
	String url3 = request.getContextPath() + "/cleanCart";
	out.write("<a href='" + url3 + "'>清空购物车</a>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
	doGet(request, response);
}
}

添加购物车

package shoppingCart;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class addCart extends HttpServlet {
private static final long serialVersionUID = 1L;
/*
 * 
 * 添加购物车
 * 
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
	//设置字符编码
	response.setContentType("text/html;charset=utf-8");
	request.setCharacterEncoding("utf-8");
	//拿到向浏览器的write对象
	PrintWriter out = response.getWriter();
	//接受“showBook”传过来的参数 id
	String id = request.getParameter("id");
	//得到的session类
	HttpSession session = request.getSession();
	Map<String, Integer> map = (Map<String, Integer>) session.getAttribute("shoppingCart");
	//如果没有session,就新建一个
	if (map == null) {
		map = new HashMap<String, Integer>();
	}
	//如果session为空,就添加传过来的商品一件
	if (map.get(id) == null) {
		map.put(id, 1);
	//否则在原有的基础上加一件
	} else {
		map.put(id, map.get(id) + 1);
	}
	//添加最新的session
	session.setAttribute("shoppingCart", map);
	//提示添加成功
	out.write("成功把商品添加到购物车!");
	//2秒刷新返回图书展示
	response.setHeader("refresh", "2;url=" + request.getContextPath() + "/showBook");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
	doGet(request, response);
}
}

购物车展示

package shoppingCart;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import bean.book;
import util.DButil;
public class showCart extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
	//设置字符编码
	response.setContentType("text/html;charset=utf-8");
	request.setCharacterEncoding("utf-8");
	//拿到向浏览器的write对象
	PrintWriter out = response.getWriter();
	out.write("=============购物车商品列表============<br><br>");
	//得到的session类
	HttpSession session = request.getSession();
	Map<String, Integer> books = (Map<String, Integer>) session.getAttribute("shoppingCart");
	//如果session为空,就提示购物车没有商品,2秒刷新返回图书展示
	if (books == null) {
		out.write("购物车没有商品!");
		response.setHeader("refresh", "2;url=" + request.getContextPath() + "/showBook");
		return;
	}//如果session不为空,遍历购物车
	for (Map.Entry<String, Integer> book : books.entrySet()) {
		book b = new book();
		b = DButil.findBookById(book.getKey());
		out.write("书名:《 " + b.getName() + "》&nbsp;&nbsp;&nbsp;作者: " + b.getAuthor() + "&nbsp;&nbsp;&nbsp;价格: "
				+ b.getPrice() + "元 &nbsp;&nbsp;&nbsp;订购数量: " + book.getValue() + "<br><br>");
	}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
	doGet(request, response);
}
}

购物车清空

package shoppingCart;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class cleanCart extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
	//设置字符编码
	response.setContentType("text/html;charset=utf-8");
	request.setCharacterEncoding("utf-8");
	//拿到向浏览器的write对象
	HttpSession session = request.getSession();
	PrintWriter out = response.getWriter();
	//清空session类,清空购物车,并提示
	session.removeAttribute("shoppingCart");
	out.write("购物车已清空!");
	//2秒刷新返回图书展示
	response.setHeader("refresh", "2;url=" + request.getContextPath() + "/showBook");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
	doGet(request, response);
}
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>shoppingCart</display-name>
<welcome-file-list>
  <welcome-file>index.html</welcome-file>
  <welcome-file>index.htm</welcome-file>
  <welcome-file>index.jsp</welcome-file>
  <welcome-file>default.html</welcome-file>
  <welcome-file>default.htm</welcome-file>
  <welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
  <description></description>
  <display-name>showBook</display-name>
  <servlet-name>showBook</servlet-name>
  <servlet-class>shoppingCart.showBook</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>showBook</servlet-name>
  <url-pattern>/showBook</url-pattern>
</servlet-mapping>
<servlet>
  <description></description>
  <display-name>addCart</display-name>
  <servlet-name>addCart</servlet-name>
  <servlet-class>shoppingCart.addCart</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>addCart</servlet-name>
  <url-pattern>/addCart</url-pattern>
</servlet-mapping>
<servlet>
  <description></description>
  <display-name>showCart</display-name>
  <servlet-name>showCart</servlet-name>
  <servlet-class>shoppingCart.showCart</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>showCart</servlet-name>
  <url-pattern>/showCart</url-pattern>
</servlet-mapping>
<servlet>
  <description></description>
  <display-name>cleanCart</display-name>
  <servlet-name>cleanCart</servlet-name>
  <servlet-class>shoppingCart.cleanCart</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>cleanCart</servlet-name>
  <url-pattern>/cleanCart</url-pattern>
</servlet-mapping>
</web-app>