Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class allItemsServlet extends HttpServlet {
- @EJB
- ItemsBeanLocal itembean;
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- List<Items> items = itembean.findAll();
- PrintWriter out = response.getWriter();
- out.println("<html>");
- out.println("<head>");
- out.println("<title>Shopping Cart</title>");
- out.println("</head>");
- out.println("<body>");
- out.println("<form action=allItemsServlet method=GET>");
- out.println("<table>");
- out.println("<tr><td>Search By:</td><td><input type=text name=searchValue></td><td><input type=submit value=Search /></tr>");
- out.println("</table>");
- out.println("<form action=allItemsServlet method=POST>");
- out.println("<input type=hidden name=action value=add>");
- out.println("<table>");
- out.println("<tr><td><b><u>Item</u></b></td><td><b><u>Quantity</u></b></td></tr>");
- int count = 0;
- String search = request.getParameter("searchValue");
- for (int i = 0; i < items.size(); i++)
- {
- if(search == null)
- {
- if (i>=0)
- out.println("</tr><tr>");
- out.println("<td>" + items.get(i).getCardname() + ":</td><td><input type=text name=item" + (i+1) + " value=0 size=3 />/" + items.get(i).getCardquantity() + "</td>");
- count++;
- }
- else
- {
- if(items.get(i).getCardname().contains(search))
- {
- if (i>=0)
- out.println("</tr><tr>");
- out.println("<td>" + items.get(i).getCardname() + ":</td><td><input type=text name=item" + (i+1) + " value=0 size=3 />/" + items.get(i).getCardquantity() + "</td>");
- count++;
- }
- }
- }
- out.println("<tr><td colspan=2><input type=submit></td></tr>");
- out.println("<input type=hidden name=count value=" + count + " />");
- out.println("</table></form>");
- out.println("</body>");
- out.println("</html>");
- }
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- HttpSession session = request.getSession();
- cartBeanLocal shoppingCart = (cartBeanLocal) session.getAttribute("cart");
- if (shoppingCart == null) {
- try
- {
- System.out.println("request new bean\n");
- Context c = new InitialContext();
- shoppingCart = (cartBeanLocal) c.lookup("java:global/12150142_v2/12150142_v2-ejb/cartBean!appBeans.cartBeanLocal");
- session.setAttribute("cart", shoppingCart);
- }
- catch (NamingException ne)
- {
- throw new ServletException(ne);
- }
- }
- PrintWriter out = response.getWriter();
- out.println("<html>");
- out.println("<head>");
- out.println("<title>Shopping Cart</title>");
- out.println("</head>");
- out.println("<body>");
- String action = request.getParameter("action");
- if (action != null && action.equals("add"))
- {
- int count = 10; //Integer.parseInt(request.getParameter("count"));
- List<Items> list = new ArrayList<Items>();
- List<Integer> values = new ArrayList<Integer>();
- int i = 1;
- while(i <= count)
- {
- list.add(itembean.find(i));
- values.add(Integer.parseInt(request.getParameter("item" + i)));
- i++;
- }
- for(int j = 0; j < list.size(); j++)
- shoppingCart.addItem(list.get(j).getCardname(), values.get(j));
- out.println(shoppingCart.getItemList());
- out.println("<h2><a href=allItemsServlet>Add more Items</a></h2>");
- out.println("<form action=ShoppingCartServlet method=POST>");
- out.println("<input type=hidden name=action value=cancel>");
- out.println("<input type=submit value=Cancel>");
- out.println("</form><br>");
- out.println("<form action=allItemsServlet method=POST>");
- out.println("<input type=hidden name=action value=checkout>");
- out.println("<input type=submit value=Checkout>");
- out.println("</form>");
- }
- else if (action != null && action.equals("cancel"))
- {
- out.println("<h2>Order cancelled</h2>");
- shoppingCart.cancel();
- session.removeAttribute("cart");
- }
- else if (action != null && action.equals("checkout"))
- {
- out.println("<h2>You checked out the following:</h2>");
- HashMap<String, Integer> items = shoppingCart.getItems();
- List<Items> list = new ArrayList<Items>();
- Set<String> keys = items.keySet();
- Iterator<String> it = keys.iterator();
- String k;
- while (it.hasNext())
- {
- k = it.next();
- if(items.get(k) > 0)
- {
- Items item = itembean.getItemByName(k);
- itembean.editAmount(item.getCardid(), items.get(k));
- }
- }
- out.println(shoppingCart.checkout());
- session.removeAttribute("cart");
- }
- out.println("<h2><a href=index.jsp>Back to start</a></h2>");
- out.println("</body>");
- out.println("</html>");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement