private void listall(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { BusinessServiceImpl service = new BusinessServiceImpl(); List<Order> list = service.getAllOrders(); request.setAttribute("orders", list); request.getRequestDispatcher("/admin/listOrder.jsp").forward(request, response); }
private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { int orderID = Integer.parseInt(request.getParameter("orderID")); BusinessServiceImpl service = new BusinessServiceImpl(); Order order = service.findOrder(orderID); service.deleteOrder(order); request.setAttribute("message", "Delete order successfully!"); } catch (Exception e) { e.printStackTrace(); request.setAttribute("message", "Delete order failed!"); } request.getRequestDispatcher("/message.jsp").forward(request, response); }
private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { BusinessServiceImpl service = new BusinessServiceImpl(); Book book; int userID; // = Integer.parseInt(request.getParameter("userId")); String purchasedBooks = ""; Date date = new Date(); Double amount = 0.00; HttpSession session = request.getSession(true); ArrayList<Book> bookList = (ArrayList<Book>) session.getAttribute("bookList"); User authenticatedUser = (User) session.getAttribute("authenticatedUser"); userID = authenticatedUser.getUserID(); if (bookList == null) { // if shopping cart is empty, cannot place order request.setAttribute("message", "Shopping cart is empty!"); } else { // compute purchasedBooks and amount book = bookList.get(0); purchasedBooks = Integer.toString(book.getBookID()); amount += book.getPrice(); for (int i = 1; i < bookList.size(); i++) { book = bookList.get(i); purchasedBooks = "," + Integer.toString(bookList.get(i).getBookID()); amount += book.getPrice(); } } Order order = new Order(); order.setUserID(userID); order.setDate(date); order.setAmount(amount); order.setPurchasedBooks(purchasedBooks); service.addOrder(order); request.setAttribute("message", "Place order successfully!"); } catch (Exception e) { e.printStackTrace(); request.setAttribute("message", "Place order failed!"); } request.getRequestDispatcher("/message.jsp").forward(request, response); }
private void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { String username = request.getParameter("username"); String password = request.getParameter("pwd"); BusinessServiceImpl service = new BusinessServiceImpl(); int userID = service.findUser(username); if (userID != 0) { out.println("<html>"); out.println("<head>"); out.println("<title>Servlet register</title>"); out.println("<link rel=\"stylesheet\" href=\"../bootstrap.min.css\">"); out.println("</head>"); out.println("<body align='center'>"); out.println("<h1>Sooooooorry!</h1>"); out.println("<h3>The username has been used. Please re-register another username.</h3>"); out.println( "<h4>---><a href='/BookMart/client/register.jsp'>Back to Register Page</a><---</h4>"); out.println("<img src='sorry.gif' />"); out.println("</body>"); out.println("</html>"); } else { if (password.isEmpty()) { out.println("<script language='javascript'>alert('Please enter a password')</script>"); } else { User user = new User(); user.setUserName(username); user.setPassword(password); service.addUser(user); userID = service.findUser(username); String url = "index.jsp?userID=" + userID; response.sendRedirect(url); } } } catch (Exception e) { e.printStackTrace(); } }
private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { int orderID = Integer.parseInt(request.getParameter("orderID")); Date date = new Date(); Double amount = Double.parseDouble(request.getParameter("amount")); String purchasedBooks = request.getParameter("purchasedBooks"); Order order = new Order(); order.setDate(date); order.setAmount(amount); order.setPurchasedBooks(purchasedBooks); BusinessServiceImpl service = new BusinessServiceImpl(); service.updateOrder(order); request.setAttribute("message", "Update order successfully!"); } catch (Exception e) { e.printStackTrace(); request.setAttribute("message", "Update order failed!"); } request.getRequestDispatcher("/message.jsp").forward(request, response); }