Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. import javax.servlet.ServletException;
  2. import javax.servlet.annotation.WebServlet;
  3. import javax.servlet.http.*;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6.  
  7. @WebServlet(name = "MyServlet")
  8. public class MyServlet extends HttpServlet {
  9.  
  10.  
  11. ArrayList<Products> prodList = new ArrayList<>();
  12.  
  13. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  14.  
  15. String url = "";
  16. String action = request.getParameter("fromPage");
  17.  
  18. /*ArrayList<Products> prodList = new ArrayList<>();
  19.  
  20. HttpSession mySession = request.getSession(false);
  21. if(mySession == null){
  22. mySession = request.getSession();
  23. mySession.setAttribute("cartSession", prodList);
  24. }*/
  25.  
  26.  
  27. if (action.equals("product1")) {
  28.  
  29. int id = Integer.parseInt(request.getParameter("prodId"));
  30. String name = request.getParameter("prodName");
  31. int quant = Integer.parseInt(request.getParameter("defQuant"));
  32. int price = Integer.parseInt(request.getParameter("prodPrice"));
  33. String tquant = request.getParameter("quantityTest");
  34.  
  35. Products prod = new Products(id, name, quant, price, tquant);
  36.  
  37. prodList.add(prod);
  38. System.out.println(prodList);
  39.  
  40. url = "/index.jsp";
  41. }
  42.  
  43. if (action.equals("product2")) {
  44.  
  45. int id = Integer.parseInt(request.getParameter("prodId"));
  46. String name = request.getParameter("prodName");
  47. int quant = Integer.parseInt(request.getParameter("defQuant"));
  48. int price = Integer.parseInt(request.getParameter("prodPrice"));
  49. String tquant = request.getParameter("quantityTest");
  50.  
  51. Products prod = new Products(id, name, quant, price, tquant);
  52.  
  53. prodList.add(prod);
  54. System.out.println(prodList);
  55.  
  56. url = "/index.jsp";
  57. }
  58.  
  59. if (action.equals("product3")) {
  60.  
  61. int id = Integer.parseInt(request.getParameter("prodId"));
  62. String name = request.getParameter("prodName");
  63. int quant = Integer.parseInt(request.getParameter("defQuant"));
  64. int price = Integer.parseInt(request.getParameter("prodPrice"));
  65. String tquant = request.getParameter("quantityTest");
  66.  
  67. Products prod = new Products(id, name, quant, price, tquant);
  68.  
  69. prodList.add(prod);
  70. System.out.println(prodList);
  71.  
  72. url = "/index.jsp";
  73. }
  74.  
  75. if (action.equals("product4")) {
  76.  
  77. int id = Integer.parseInt(request.getParameter("prodId"));
  78. String name = request.getParameter("prodName");
  79. int quant = Integer.parseInt(request.getParameter("defQuant"));
  80. int price = Integer.parseInt(request.getParameter("prodPrice"));
  81. String tquant = request.getParameter("quantityTest");
  82.  
  83. Products prod = new Products(id, name, quant, price, tquant);
  84.  
  85. prodList.add(prod);
  86. System.out.println(prodList);
  87.  
  88. url = "/index.jsp";
  89. }
  90.  
  91. if (action.equals("checkout")) {
  92.  
  93. String ck = "";
  94.  
  95. HttpSession mySession = request.getSession(true);
  96.  
  97. mySession.setAttribute("cartSession", prodList);
  98. int index = 0;
  99.  
  100.  
  101. for (int i = 0; i < prodList.size(); i++) {
  102. ck += prodList.get(i).toString();
  103. String cTruck = "cybertruck";
  104. if(ck.contains(cTruck)){
  105. index++;
  106. System.out.println("cybertruck detected");
  107. System.out.println(index);
  108.  
  109. }
  110. }
  111. System.out.println(ck);
  112.  
  113. Cookie myCookie = new Cookie("ShoppingCartCookie", ck);
  114. myCookie.setMaxAge(60 * 60);
  115. response.addCookie(myCookie);
  116.  
  117. System.out.println(myCookie.getName() + " " + myCookie.getValue());
  118.  
  119. url = "/index.jsp";
  120. }
  121.  
  122. getServletContext().getRequestDispatcher(url).forward(request, response);
  123. }
  124. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  125.  
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement