一、购物车的实现流程
购物车流程| ProcessOn免费在线作图,在线流程图,在线思维导图
二、创建Book类,封装图书信息
public class Book implements Serializable {
private static final long serialVersionUID = 1L;
private String id; //书的编号
private String name; //书名
public Book() {
}
public Book(String id, String name) {
this.id = id;
this.name = name;
}
//补上get set
三、创建数据库模拟类BookDB
public class BookDB {
private static Map<String, Book> books = new LinkedHashMap<String, Book>();
static {
books.put("1", new Book("1", "javaweb开发"));
books.put("2", new Book("2", "jdbc开发"));
books.put("3", new Book("3", "java基础"));
books.put("4", new Book("4", "struts开发"));
books.put("5", new Book("5", "spring开发"));
}
// 获得所有的图书
public static Collection<Book> getAll() {
return books.values();
}
// 根据指定的id获得图书
public static Book getBook(String id) {
return books.get(id);
}
}
四、创建Servlet
1.ListBookServlet
显示所有可购买图书,并提供购买链接
public class ListBookServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
Collection<Book> books = BookDB.getAll();
out.write("本站提供的图书有:<br>");
for (Book book : books) {
String url = req.getContextPath()+"/addBookToCartServlet?id=" + book.getId();
out.write(book.getName() + "<a href='" + url
+ "'>点击加入购物车</a><br>");
//String url = req.getContextPath()+"/addBookToCartServlet?id=" + book.getId();
//String newUrl=resp.encodeURL(url);
//out.write(book.getName() + "<a href='" + newUrl + "'>点击加入购物车</a><br>");
}
}
}
2.AddBookToCartServlet
a.将用户购买的图书放入购物车,购物车保存到Session对象中;
b.重定向到用户已经加入购物车的图书列表;
public class AddBookToCartServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// 获得用户要加入购物车的图书id
String id = req.getParameter("id");
if (id == null) {
// 如果id为null,重定向到ListBookServlet页面
String url = req.getContextPath()+"/listBookServlet";
resp.sendRedirect(url);
return;
}
Book book = BookDB.getBook(id);
// 创建或者获得用户的Session对象
HttpSession session = req.getSession();
// 从Session对象中获得用户的购物车
List<Book> cart = (List) session.getAttribute("cart");
if (cart == null) {
// 首次购买,为用户创建一个购物车(List集合模拟购物车)
cart = new ArrayList<Book>();
// 将购物车存入Session对象
session.setAttribute("cart", cart);
}
// 将商品放入购物车
cart.add(book);
// 创建Cookie存放Session的标识号
Cookie cookie = new Cookie("JSESSIONID", session.getId());
cookie.setMaxAge(60 * 30);
cookie.setPath(req.getContextPath());
resp.addCookie(cookie);
// 重定向到购物车页面
String url = req.getContextPath()+"/cartServlet";
resp.sendRedirect(url);
//String newurl = resp.encodeRedirectURL(url);
//resp.sendRedirect(newurl);
}
}
3.CartServlet
展示用户已经加入购物车的图书
public class CartServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
// 变量cart引用用户的购物车
List<Book> cart = null;
// 变量pruFlag标记用户是否加入过图书到购物车
boolean purFlag = true;
// 获得用户的session
HttpSession session = req.getSession(false);
// 如果session为null,purFlag置为false
if (session == null) {
purFlag = false;
} else {
// 获得用户购物车
cart = (List) session.getAttribute("cart");
// 如果用户的购物车为null,purFlag置为false
if (cart == null) {
purFlag = false;
}
}
/*
* 如果purFlag为false,表明用户没有加入过图书到购物车,重定向到ListServlet页面
*/
if (!purFlag) {
out.write("对不起!您还没有加入任何图书到购物车!<br>");
resp.sendRedirect(req.getContextPath()+"/listBookServlet");
} else {
// 否则显示用户购物车里图书的信息
out.write("您购物车里的图书有:<br>");
for (Book book : cart) {
out.write(book.getName() + "<br>");
}
}
}
}
五、运行项目,查看结果
http://localhost:8080/javaweb-demo/ListBookServlet
点击链接加入购物车,结果如下:
注意:Session ID是保存在Cookie中,Cookie设置了有效时间,那么在有效时间内,即使用户关闭浏览器再打开浏览器,服务器也能收到SessionID,也就能找到Session对象,如果该对象没有失效。