Ejemplo n.º 1
0
  /** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    try {
      int result = -1;
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();

      HttpSession session = request.getSession();

      // Get request parameters for userID and password
      String userName = request.getParameter("userName");
      String password = request.getParameter("password");

      if ((result = LoginDao.validate(userName, password)) != -1) {

        User userObj = UserDAO.getUser(userName);

        // Set session parameters
        session = request.getSession(true);
        session.setAttribute("user", userName);
        session.setAttribute("userID", result);

        // Setting session to expiry in 30 mins
        session.setMaxInactiveInterval(30 * 60);
        Cookie cookieUserName = new Cookie("user", userName);
        cookieUserName.setMaxAge(30 * 60);
        response.addCookie(cookieUserName);

        RequestDispatcher rd;
        if (userObj.getIsAdmin()) {
          rd = request.getRequestDispatcher("AdminServlet");
        } else {
          // Get all discounted products
          request.setAttribute("discountedProductList", this.getAllDiscoutedProducts(result));

          // Get non-discounted products
          request.setAttribute("nonDiscountedProductList", this.getAllNonDiscoutedProducts(result));

          // Get the user membership
          Map<String, String> userCategory = UserDAO.getUserCategory(result);
          request.setAttribute("userCategoryID", Integer.parseInt(userCategory.get("categoryID")));

          rd = request.getRequestDispatcher("loginSuccess.jsp");
        }
        rd.forward(request, response);

      } else {
        request.setAttribute("error", "Invalid Username or Password. Please try again.");
        RequestDispatcher rd = request.getRequestDispatcher("/login.jsp");
        rd.include(request, response);
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Ejemplo n.º 2
0
  /** Function to get all the discounted products */
  public List<Product> getAllDiscoutedProducts(int userID) {
    List<Product> discountedProducts = null;
    try {

      discountedProducts = ProductDao.getDiscountedProducts(userID);
      Map<String, String> userCategory = UserDAO.getUserCategory(userID);
      for (int i = 0; i < discountedProducts.size(); i++) {
        double productPrice = discountedProducts.get(i).getProductPrice();
        double x = productPrice * (Double.parseDouble(userCategory.get("categoryDiscount")) / 100);
        double y = productPrice - x;
        discountedProducts.get(i).setProductDiscountedPrice(y);
      }

    } catch (Exception e) {

      e.printStackTrace();
    }
    return discountedProducts;
  }