Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.*;
- import java.io.IOException;
- import java.util.ArrayList;
- @WebServlet(name = "MyServlet")
- public class MyServlet extends HttpServlet {
- ArrayList<Products> prodList = new ArrayList<>();
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- String url = "";
- String action = request.getParameter("fromPage");
- /*ArrayList<Products> prodList = new ArrayList<>();
- HttpSession mySession = request.getSession(false);
- if(mySession == null){
- mySession = request.getSession();
- mySession.setAttribute("cartSession", prodList);
- }*/
- if (action.equals("product1")) {
- int id = Integer.parseInt(request.getParameter("prodId"));
- String name = request.getParameter("prodName");
- int quant = Integer.parseInt(request.getParameter("defQuant"));
- int price = Integer.parseInt(request.getParameter("prodPrice"));
- String tquant = request.getParameter("quantityTest");
- Products prod = new Products(id, name, quant, price, tquant);
- prodList.add(prod);
- System.out.println(prodList);
- url = "/index.jsp";
- }
- if (action.equals("product2")) {
- int id = Integer.parseInt(request.getParameter("prodId"));
- String name = request.getParameter("prodName");
- int quant = Integer.parseInt(request.getParameter("defQuant"));
- int price = Integer.parseInt(request.getParameter("prodPrice"));
- String tquant = request.getParameter("quantityTest");
- Products prod = new Products(id, name, quant, price, tquant);
- prodList.add(prod);
- System.out.println(prodList);
- url = "/index.jsp";
- }
- if (action.equals("product3")) {
- int id = Integer.parseInt(request.getParameter("prodId"));
- String name = request.getParameter("prodName");
- int quant = Integer.parseInt(request.getParameter("defQuant"));
- int price = Integer.parseInt(request.getParameter("prodPrice"));
- String tquant = request.getParameter("quantityTest");
- Products prod = new Products(id, name, quant, price, tquant);
- prodList.add(prod);
- System.out.println(prodList);
- url = "/index.jsp";
- }
- if (action.equals("product4")) {
- int id = Integer.parseInt(request.getParameter("prodId"));
- String name = request.getParameter("prodName");
- int quant = Integer.parseInt(request.getParameter("defQuant"));
- int price = Integer.parseInt(request.getParameter("prodPrice"));
- String tquant = request.getParameter("quantityTest");
- Products prod = new Products(id, name, quant, price, tquant);
- prodList.add(prod);
- System.out.println(prodList);
- url = "/index.jsp";
- }
- if (action.equals("checkout")) {
- String ck = "";
- HttpSession mySession = request.getSession(true);
- mySession.setAttribute("cartSession", prodList);
- int index = 0;
- for (int i = 0; i < prodList.size(); i++) {
- ck += prodList.get(i).toString();
- String cTruck = "cybertruck";
- if(ck.contains(cTruck)){
- index++;
- System.out.println("cybertruck detected");
- System.out.println(index);
- }
- }
- System.out.println(ck);
- Cookie myCookie = new Cookie("ShoppingCartCookie", ck);
- myCookie.setMaxAge(60 * 60);
- response.addCookie(myCookie);
- System.out.println(myCookie.getName() + " " + myCookie.getValue());
- url = "/index.jsp";
- }
- getServletContext().getRequestDispatcher(url).forward(request, response);
- }
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement