首页 > 编程学习 > session实现购物车操作

session实现购物车操作

发布时间:2022/1/17 12:29:13

1.新建一个entity实体包,里面存放Book的信息

Book.java里的内容:

package com.itheima.entity;

import java.io.Serializable;

public class Book implements Serializable{
	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 + "]";
	}
	
	
}

2.新建一个util包,关联数据库,这里没有关联数据库,直接引入本地的书籍


DBUtil.java内容:

package com.itheima.util;

import java.util.HashMap;
import java.util.Map;

import com.itheima.entity.Book;


public class DBUtil {
	private static Map<String, Book> books = new HashMap<String, Book>();
	
	static{
		books.put("1", new Book("1", "谁动了我的奶酪", 20, "斯宾塞·约翰逊"));
		books.put("2", new Book("2", "唤醒心中的巨人", 20, "安东尼·罗宾斯"));
		books.put("3", new Book("3", "做最好的自己", 30, "李开复"));
		books.put("4", new Book("4", "时间简史", 10, "霍金"));
	}
	
	//�õ�������
	public static Map<String, Book> findAllBooks(){
		return books;
	}
	
	/**
	 * ���id����ָ������
	 * @param id
	 * @return
	 */
	public static Book findBookById(String id){
		return books.get(id);
	}
}
3.新建购物车包,包含展现所有图书信息,添加图书,和查看购物车三个操作

(1)ShowAllBooksServlet.java

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

public class ShowAllBooksServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		request.getSession();
		out.print("本网站有以下好书<br/>");
		Map<String, Book> books = DBUtil.findAllBooks();
		for (Map.Entry<String, Book> book : books.entrySet()) {
			String url = request.getContextPath()+"/servlet/addCart?id="+book.getKey();
			out.print("<a href='"+response.encodeURL(url)+"' >"+book.getValue().getName()+"</a><br/>");
		}
		String url2 = request.getContextPath()+"/servlet/showCart";
		out.print("<a href='"+response.encodeURL(url2)+"'>查看购物车</a>");
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
(2)AddCart.java

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

public class AddCart extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		//根据id得到书
		String id = request.getParameter("id");
		Book book = DBUtil.findBookById(id);
		//得到session对象
		 HttpSession session = request.getSession();
		//从session中取出list(购物车)
		List<Book> list = (List<Book>)session.getAttribute("cart");
		if(list==null){
			list = new ArrayList<Book>();
		}
		list.add(book);
		
		session.setAttribute("cart", list);//把list放回session域中
		
		out.print("购物成功");
		String url = request.getContextPath()+"/servlet/showAllBooksServlet";
		//response.setHeader("refresh", "2;url="+response.encodeURL(url));
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}

(3)ShowCart.java

package com.itheima.cart;

import java.io.IOException;
import java.io.PrintWriter;
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.itheima.entity.Book;

public class ShowCart extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.print("购物车有以下商品:<br/>");
		 HttpSession session = request.getSession();
		//得打session对象
		List<Book> books = (List<Book>)session.getAttribute("cart");
		if(books==null){
			out.print("你还什么都没买呢");
			response.setHeader("refresh", "2;url="+request.getContextPath()+"/servlet/showAllBooksServlet");
			//response.sendRedirect(request.getContextPath()+"/servlet/showAllBooksServlet");
			return;
		}
		for (Book book : books) {
			out.write(book.getName()+"<br/>");
			
		}
		
		//session.setMaxInactiveInterval(10);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}




Copyright © 2010-2022 ngui.cc 版权所有 |关于我们| 联系方式| 豫B2-20100000