以购买图书为例,使用session模拟实现购物车,定义了5个类:
(1)Book类:此类用于封装图书的信息,定义id和name属性,分别表示编号和名称
(2)BookDB类:模拟保存所有图书的数据库,此类实现时通过Map集合存储了5个不同的Book对象,提供了指定图书和所有图书的相关方法
(3)ListBookServlet:显示所有可购买图书的列表,通过单击购买链接,即可将指定图书加入购物车
(4)PurchaseServlet:此类有两个功能:一是将用户购买的图书信息保存到session对象中去,二是用户购买图书结束后,将页面重定向到用户已经购买的图书列表
(5)CartServlet:购物车类 用于展示用户购买的图书列表
package cn.edu.bdu.mc02;
/*
* 此类用于封装图书的信息,定义id和name属性,分别表示编号和名称
*/
public class Book {
private String id;
private String name;
public Book(String id, String name) {
this.id=id;
this.name=name;
}
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;
}
}
----------------------------------------------------------------------------------------------------------------------------
package cn.edu.bdu.mc02;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
/*模拟保存所有图书的数据库
* 此类实现时通过Map集合存储了5个不同的Book对象,提供了指定图书和所有图书的相关方法
*/
public class BookDB {
private static Map
static{
books.put("1", new Book("1","java开发"));
books.put("2", new Book("2","java基础"));
books.put("3", new Book("3","java1"));
books.put("4", new Book("4","java2"));
books.put("5", new Book("5","java3"));
}
//获取所有的图书
public static Collection
return books.values();
}
//根据指定的id获取图书
public static Book getBook(String id) {
return books.get(id);
}
}
---------------------------------------------------------------------------------------------------------------------------------------------
package cn.edu.bdu.mc02;
/*
* 显示所有可购买图书的列表,通过单击购买链接,即可将指定图书加入购物车
*/
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ListBookServlet
*/
@WebServlet("/ListBookServlet")
public class ListBookServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ListBookServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out =response.getWriter();//定义一个输出语句
Collection
out.print("本站提供的书有:");
for(Book book:books){
String url="/chapter006/PurchaseServlet?id="+book.getId();
out.write(book.getName()+"点击购买
");
}
String url="/chapter006/PurchaseServlet";
out.print("我的购物车
");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------
package cn.edu.bdu.mc02;
/*
* 此类有两个功能:一是将用户购买的图书信息保存到session对象中去,二是用户购买图书结束后,将页面重定向到
* 用户已经购买的图书列表
*
*
* cart 第二次浏览的时候 cart已经存在
*
*/
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class PurchaseServlet
*/
@WebServlet("/PurchaseServlet")
public class PurchaseServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public PurchaseServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获得用户购买的商品
String id=request.getParameter("id");
if(id==null){
//如果id为空就重定向到ListBookServlet
String url="/chapter006/ListBookServlet";
response.sendRedirect(url);
return;
}
Book book=BookDB.getBook(id);
//创建或者获得用户的Session对象
HttpSession session=request.getSession();
//从session对象中获得用户的购物车
//List
List
if(cart==null){
//首次购买,为用户创建一个购物车(List集合模拟购物车)
cart=new ArrayList
//将购物车存入session对象
session.setAttribute("cart", cart);
}
//将商品放入购物车
cart.add(book);
//创建cookie存放session的标识号
Cookie cookie=new Cookie("JESSIONID", session.getId());
cookie.setMaxAge(60*30);
cookie.setPath("/chapter006");
response.addCookie(cookie);
//重定向到购物车页面
String url="/chapter006/CartServlet";
response.sendRedirect(url);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
package cn.edu.bdu.mc02;
/*
* 用于展示用户购买的图书列表
*/
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class CartServlet
*/
@WebServlet("/CartServlet")
public class CartServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public CartServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
HttpSession session=request.getSession(false);
List
if(cart!=null&&cart.size()>0){
out.print("您购买的图书有:");
out.print("图书列表
");
for(Book book:cart){
out.print(book.getName()+"
");
}
}
else{
out.print("对不起您还没有购买图书!");
out.print("图书列表
");
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}