JavaWeb_세 션 으로 간편 카 트 구현

5194 단어 Java
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");
	}
}

좋은 웹페이지 즐겨찾기