Session实现购物车模拟数据库购买图书


1、创建封装图书信息类(Book.java),用于封装图书信息,定义id和name属性,分别用来表示书的编号和名称。

public class Book {
    private String id;
    private String 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;
    }
    public Book(String id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
    public Book() {
        super();
        // TODO Auto-generated constructor stub
    }
    @Override
    public String toString() {
        return "Book [id=" + id + ", name=" + name + "]";
    }
}

2、创建数据库模拟类(DbUtil.java),用于模拟保存所有图书的数据库。

public class DbUtil {
    private static Map<String,Book> books=new LinkedHashMap<String,Book>();
    static{
        books.put("1",new Book("1","Python"));
        books.put("2",new Book("2","Android"));
        books.put("3",new Book("3","MySQL"));
    }
    public static Collection<Book> getAll(){
        return books.values();
    }
    public static Book getbyId(String id){
        return books.get(id);
    }
}

3、创建Servlet(ListBooks.java),用于显示所有可购买图书的列表,通过点击“购买”链接,便将指定的图书添加到购物车中。

public class ListBooks extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        Collection<Book> books=DbUtil.getAll();
        out.write("可以购买的图书有:<br/>");
        for(Book book:books){
            String url="/ShopCart/BuyAction?id="+book.getId();
            out.write(book.getName()+"<a href='"+url+"'>购买</a><br/>");
        }
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doGet(request,response);
    }
}

4、创建一个Servlet类(BuyAction.java),实现了两个功能:一个是讲用户购买的图书信息保存到Session对象中,另一个是用户在购买图书结束后,讲页面重定向到用户已经购买的图书列表。该类在实现时,通过ArrayList集合模拟了一个购物车,然后将购买的所有图书添加到购物车中,最后通过Session对象传递给CartAction,由CartAction展示用户已经购买的图书。

public class BuyAction extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String id = request.getParameter("id");
        if(id==null){
            String url="/ShopCart/ListBooks";
            response.sendRedirect(url);
            return;
        }
        Book book = DbUtil.getbyId(id);
        HttpSession session=request.getSession();
        List<Book> cart=(List<Book>) session.getAttribute("cart");
        if(cart==null){
            cart = new ArrayList<Book>();
            session.setAttribute("cart", cart);
        }
        cart.add(book);
        Cookie cookie =new Cookie("JSESSIONID",session.getId());
        cookie.setMaxAge(60);
        cookie.setPath("/ShopCart");
        response.sendRedirect("/ShopCart/CartAction");
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doGet(request, response);
    }
}

5、创建一个Servlet类(CartAction.java),用于展示用户已经购买的图书列表。
public class CartAction extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=utf-8");
    PrintWriter out = response.getWriter();
    List<Book> cart=null;
    boolean flag=true;
    HttpSession session =request.getSession();
    if(session==null){
        flag=false;
    }else{
        cart=(List<Book>) session.getAttribute("cart");
        if(cart==null){
            flag=false;
        }
    }
    if(!flag){
        out.write("看得多,买的少");
    }else{
        out.write("你买的书有:"+"<br/>");
        for(Book book:cart){
            out.write(book.getName()+"<br/>");
        }
    }
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    this.doGet(request, response);
}

}

声明:子客|版权所有,违者必究|如未注明,均为原创|网站采用BY-NC-SA协议授权

转载:请注明原文链接 - Session实现购物车模拟数据库购买图书


Carpe Diem and Do what I like