53 lines
1.7 KiB
Java
53 lines
1.7 KiB
Java
|
package example.controller;
|
||
|
|
||
|
import example.model.Computer;
|
||
|
import example.service.ICartService;
|
||
|
import example.service.impl.CartServiceImpl;
|
||
|
|
||
|
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 java.io.IOException;
|
||
|
import java.util.List;
|
||
|
|
||
|
@WebServlet("/cart")
|
||
|
public class CartServlet extends HttpServlet {
|
||
|
|
||
|
ICartService cartService = new CartServiceImpl();
|
||
|
|
||
|
@Override
|
||
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||
|
String action = req.getParameter("action");
|
||
|
switch (action){
|
||
|
case "list":
|
||
|
try {
|
||
|
toCart(req, resp);
|
||
|
} catch (Exception e) {
|
||
|
throw new RuntimeException(e);
|
||
|
}
|
||
|
break;
|
||
|
case "add":
|
||
|
String id = req.getParameter("id");
|
||
|
req.getSession().setAttribute("cart",id);
|
||
|
resp.sendRedirect("/cart");
|
||
|
break;
|
||
|
case "remove":
|
||
|
String removeId = req.getParameter("id");
|
||
|
req.getSession().removeAttribute(removeId);
|
||
|
resp.sendRedirect("/cart");
|
||
|
break;
|
||
|
default:
|
||
|
req.getRequestDispatcher("/WEB-INF/jsp/cart.jsp").forward(req,resp);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void toCart(HttpServletRequest req, HttpServletResponse resp) throws Exception {
|
||
|
List<Computer> computers = cartService.myCart(16);
|
||
|
req.setAttribute("computers",computers);
|
||
|
req.getRequestDispatcher("cart.jsp").forward(req,resp);
|
||
|
}
|
||
|
}
|