JavaWeb学习-会话管理系列-8-Session练习-模拟购物车实现过程

前面学习cookie的时候,做了一个模拟浏览历史纪录的练习,这篇学习session,我们来再前面知识基础上,做一个模拟购物车的场景。前面文章中Book.java和DBUtil.java都需要拿过来使用。

1.文件结构

JavaWeb学习-会话管理系列-8-Session练习-模拟购物车实现过程_第1张图片

ShowAllBooksServlet.java是显示全部商品列表页面,提供点击一本书,点击效果就是模拟添加购物车功能。AddCart.java就是处理添加购物车逻辑,showCart.java就是显示购物页面。

 

2.文件代码

web.xml



  SessionDemo
  

  
    showAllBooksServlet
    com.anthony.cart.ShowAllBooksServlet
  
  
    showAllBooksServlet
    /showAllBook
  
  
  
    addCart
    com.anthony.cart.AddCart
  
  
    addCart
    /addCart
  
  
  
    showCart
    com.anthony.cart.ShowCart
  
  
    showCart
    /showCart
  
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  

DBUtil.java

package com.anthony.util;
 
import java.util.HashMap;
import java.util.Map;
 
import com.anthony.entity.Book;
 
public class DBUtil {
	
	private static Map books = new HashMap();
	
	//这里不走数据库查询,直接生成几本书来模拟下
	static {
		books.put("1", new Book("1", "西游记", 16.9, "吴承恩"));
		books.put("2", new Book("2", "红楼梦", 37.3, "曹雪芹"));
		books.put("3", new Book("3", "三国演义", 25.8, "罗贯中"));
		books.put("4", new Book("4", "水浒传", 33.0, "施耐庵"));
	}
	
	//得到所有图书
	public static Map findAllBooks() {
		return books;
	}	
	
	//根据id查找到书
	public static Book findBookById(String id) {
		return books.get(id);
	}
}

Book.java

package com.anthony.entity;
 
public class Book {
	private String id;
	private String name;
	private double price;
	private String author;
	
	public Book(String id, String name, double price, String author) {
		super();
		this.id = id;
		this.name = name;
		this.price = price;
		this.author = author;
	}
	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;
	}
	@Override
	public String toString() {
		return "Book [id=" + id + ", name=" + name + ", price=" + price + ", author=" + author + "]";
	}
	
}

ShowAllBooksServlet.java

package com.anthony.cart;

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 com.anthony.entity.Book;
import com.anthony.util.DBUtil;

public class ShowAllBooksServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html;charset=UTF-8");
		PrintWriter out = resp.getWriter();
		
		//显示本网站全部商品列表
		out.print("本网站有以下好书:
"); Map books = DBUtil.findAllBooks(); for (Map.Entry b : books.entrySet()) { out.print(""+b.getValue().getName()+"
"); } //点击查看购物车 out.print("
查看购物车
"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }

AddCart.java

package com.anthony.cart;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

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 com.anthony.entity.Book;
import com.anthony.util.DBUtil;

public class AddCart extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html;charset=UTF-8");
		PrintWriter out = resp.getWriter();
		//根据id得到书
		String id = req.getParameter("id");
		Book book = DBUtil.findBookById(id);
		
		//从session中取出list(购物车)
		HttpSession ss = req.getSession();
		List list = (List) ss.getAttribute("cart");
		//判断list为空
		if(list == null) {
			list = new ArrayList();
		}
		list.add(book);
		out.print("购买成功!");
		//把cart添加到session
		ss.setAttribute("cart", list);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
	
	
}

ShowCart.java

package com.anthony.cart;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
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 com.anthony.entity.Book;
import com.anthony.util.DBUtil;

public class ShowCart extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html;charset=UTF-8");
		PrintWriter out = resp.getWriter();
		//得到session对象
		List list = (List) req.getSession().getAttribute("cart");
		//遍历list
		if(list == null) {
			list = new ArrayList();
			out.print("您目前还添加商品到购物车!");
			//重定向到商品页面
			resp.setHeader("refresh", "2;url="+req.getContextPath()+"/showAllBook");
			return;
		}
		for (Book book : list) {
			out.print(book.getName() + "
"); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }

3.测试

浏览器打开http://localhost:8080/SessionDemo/showAllBook

JavaWeb学习-会话管理系列-8-Session练习-模拟购物车实现过程_第2张图片

点击一本书名,相当于点击加入购物车操作。

JavaWeb学习-会话管理系列-8-Session练习-模拟购物车实现过程_第3张图片

返回showAllBooks页面,点击查看购物车

JavaWeb学习-会话管理系列-8-Session练习-模拟购物车实现过程_第4张图片

如果第一次没有点击直接看购物车,会打印没有添加商品,2秒后重定向到showAllBooks页面。

你可能感兴趣的:(JavaWeb学习-会话管理系列-8-Session练习-模拟购物车实现过程)