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");
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JPA + QueryDSL 계층형 댓글, 대댓글 구현(2)이번엔 전편에 이어서 계층형 댓글, 대댓글을 다시 리팩토링해볼 예정이다. 이전 게시글에서는 계층형 댓글, 대댓글을 구현은 되었지만 N+1 문제가 있었다. 이번에는 그 N+1 문제를 해결해 볼 것이다. 위의 로직은 이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.