Example #1
0
  /** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String command;
    User user;
    String url = MAIN_JSP;

    command = request.getParameter(COMMAND);

    System.out.println("Command: " + command);

    if (command != null) {
      if (command.equalsIgnoreCase(CREATE_USER_CMD)) {

        user = createUserFromRequest(request);
        user.validateUser();
        if (user.isUserValid()) {
          userDatabaseAccessor.insertUser(user);
          url = MAIN_JSP;
        } else {
          url = USER_PROFILE_JSP;
        }
        request.getSession().setAttribute(SESSION_USER, user);
      } else if (command.equalsIgnoreCase(UPDATE_USER_CMD)) {
        user = createUserFromRequest(request);
        user.validateUser();
        if (user.isUserValid()) {
          userDatabaseAccessor.updateUser(user);
          @SuppressWarnings("unchecked")
          List<Book> list = (List<Book>) request.getSession().getAttribute(SESSION_LIST);
          if (list == null) {
            list = bookDatabaseAccessor.getSpecialBooks();
            request.getSession().setAttribute(SESSION_LIST, list);
          }
          url = MAIN_JSP;
        } else {
          url = USER_PROFILE_JSP;
        }
        request.getSession().setAttribute(SESSION_USER, user);
      } else if (command.equalsIgnoreCase(DISPLAY_MAIN_PAGE_CMD)) {
        List<Book> list = getBooksFromRequest(request);
        url = MAIN_JSP;
        request.getSession().setAttribute(SESSION_LIST, list);
      }
    }
    System.out.println("url: " + url);
    System.out.println(getServletContext().toString());

    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
    dispatcher.forward(request, response);
  }
Example #2
0
  private User createUserFromRequest(HttpServletRequest request) {
    System.out.println("Email: " + request.getParameter("emailAddress"));

    User user = (User) request.getSession().getAttribute(SESSION_USER);
    if (user == null) {
      user = new User();
    }
    user.setEmail(request.getParameter("emailAddress"));
    user.setFirstName(request.getParameter("firstName"));
    user.setLastName(request.getParameter("lastName"));
    user.setPassword(request.getParameter("password"));
    user.setAddressFirstLine(request.getParameter("addrFirstLine"));
    user.setAddressSecondLine(request.getParameter("addrSecondLine"));
    user.setCity(request.getParameter("addrCity"));
    user.setState(request.getParameter("addrState"));
    user.setZipcode(request.getParameter("addrZip"));

    return user;
  }