JavaWeb实现模拟购物车

package cn.itcast.chapter05.session.example01;
import java.io.Serializable;
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;
    	}
    	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.itcast.chapter05.session.example01;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
public class BookDB {
	 private static Map<String,Book>books=new LinkedHashMap<String,Book>();
	 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<Book>getAll(){
	        return books.values();
	    }
	  //根据指定的id获取图书
	    public static Book getBook(String id) {
	        return books.get(id);        
	    }

}

package cn.itcast.chapter05.session.example01;

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;

/**
 * Servlet implementation class 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 {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();
        List<Book> cart=null;
        boolean purFlag=true;
        HttpSession session=request.getSession(false);
        if(session==null){
        	purFlag=false;
        }
        else{
        	cart=(List) session.getAttribute("cart");
        	if(cart==null){
        		purFlag=false;
        	}
        }
        if(!purFlag){
        	out.print("对不起您还没有购买图书!
"
); out.print("图书列表
"
); }else{ out.print("您购买的图书有:"); //double price=0; for(Book book:cart){ out.print(book.getName()+"
"
); //out.print("图书列表
");
} } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } }
package cn.itcast.chapter05.session.example01;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class 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 {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out =response.getWriter();//定义一个输出语句
		Collection<Book>books=BookDB.getAll();
		out.print("本站提供的书有:");
		for(Book book:books){
			String url="/chapter05/PurchaseServlet?id="+book.getId();
			out.write(book.getName()+"点击购买
"
);} } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } }
package cn.itcast.chapter05.session.example01;

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

import javax.servlet.ServletException;
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
 */
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 {
		// TODO Auto-generated method stub
		//获得用户购买的商品
		String id=request.getParameter("id");
		if(id==null){
			//如果id为空就重定向到ListBookServlet
			String url="/chapter05/ListBookServlet";
			response.sendRedirect(url);
			return;
			}
		Book book=BookDB.getBook(id);
		//创建或者获得用户的Session对象
		HttpSession session=request.getSession();
		//从session对象中获得用户的购物车
		//Listcart =(List)session.getAttribute("cart");
		List<Book>cart=(List<Book>)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("JESSIONID", session.getId());
		cookie.setMaxAge(60*30);
		cookie.setPath("/chapter05");
		response.addCookie(cookie);
		//重定向到购物车页面
		String url="/chapter05/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
	}

}

JavaWeb实现模拟购物车_第1张图片
JavaWeb实现模拟购物车_第2张图片

你可能感兴趣的:(Java,Web,java,开发语言,后端)