JavaWeb_利用Session实现简易购物车

package com.Servlet;

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

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.Product.Product;

/**
 * Servlet implementation class MainServlet
 */
@WebServlet(name = "MainServlet", urlPatterns = { "/ProductInfor","/ShowShopCar","/ShowShopHall","/PushShopCar" })
public class MainServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MainServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
	/**
	 * @see Servlet#init(ServletConfig)
	 */
    public List products1 = new ArrayList();
    public List products2 = null;
    public List numbers = null;

	@Override
	public void init() throws ServletException {
		products1.add(new Product(1, "C语言", "清华出版社", 25.50F));
		products1.add(new Product(2, "Java", "清华出版社", 65.50F));
		products1.add(new Product(3, "C#", "清华出版社", 15.50F));
		products1.add(new Product(4, "汇编语言", "清华出版社", 85.50F));
		products1.add(new Product(5, "UML", "清华出版社", 36.50F));
		products1.add(new Product(6, "C++", "清华出版社", 50.50F));
		products1.add(new Product(7, "计算机组成原理", "清华出版社", 25.50F));
		products1.add(new Product(8, "数据库", "清华出版社", 68.50F));
	}
	@SuppressWarnings("unused")
	private Product getProduct(int id){
		for(Product product : products1){
			if(product.getId() == id){
				return product;
			}
		}
		return null;
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String uri = request.getRequestURI();
		if (uri.endsWith("/ShowShopCar")) {
			ShowShopCar(request, response);
		}else if (uri.endsWith("/ProductInfor")) {
			ProductInfor(request, response);
		}else if (uri.endsWith("/ShowShopHall")) {
			ShowShopHall(request, response);
		}else if (uri.endsWith("/PushShopCar")) {
			PushShopCar(request, response);
		}
	}
	private void ShowShopHall(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.getSession().setAttribute("list1",products1);
		response.sendRedirect("MainHall.jsp");
	}
	private void ShowShopCar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		@SuppressWarnings("unchecked")
		ArrayList products2 = (ArrayList)request.getSession().getAttribute("list2");
		@SuppressWarnings("unchecked")
		ArrayList numbers = (ArrayList)request.getSession().getAttribute("number");
		request.getSession().setAttribute("number",numbers);
		request.getSession().setAttribute("list2",products2);
		response.sendRedirect("ShowShopCar.jsp");
	}
	private void ProductInfor(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		Object[] p = products1.toArray();
		request.getSession().setAttribute("book",((Product) p[Integer.parseInt(request.getParameter("id"))-1]));
		response.sendRedirect("ProductInfor.jsp");
	}
	private void PushShopCar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		@SuppressWarnings("unchecked")
		ArrayList products2 = (ArrayList)request.getSession().getAttribute("list2");
		@SuppressWarnings("unchecked")
		ArrayList numbers = (ArrayList)request.getSession().getAttribute("number");
		if(products2==null&&numbers==null){
			products2 = new ArrayList();
			numbers = new ArrayList();
			products2.add((Product)request.getSession().getAttribute("book"));
			numbers.add(request.getParameter("number"));
			request.getSession().setAttribute("list2",products2);
			request.getSession().setAttribute("number",numbers);
		}else{
			products2.add((Product)request.getSession().getAttribute("book"));
			request.getSession().setAttribute("list2",products2);
			numbers.add(request.getParameter("number"));
			request.getSession().setAttribute("number",numbers);
		}
		response.sendRedirect("ShowShopCar.jsp");
	}
}

你可能感兴趣的:(Java)