@Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String url = "/index.html";

    // get current action
    String action = request.getParameter("action");
    if (action == null) {
      action = "join"; // default action
    }

    // perform action and set URL to appropriate page
    if (action.equals("join")) {
      url = "/index.jsp"; // the "join" page
    } else if (action.equals("add")) {
      // get parameters from the request
      String firstName = request.getParameter("firstName");
      String lastName = request.getParameter("lastName");
      String email = request.getParameter("email");

      // store data in User object
      User user = new User();
      user.setEmail(email);
      user.setFirstName(firstName);
      user.setLastName(lastName);

      // validate the parameters
      String message;
      if (UserDB.emailExists(user.getEmail())) {
        message = "This email address already exists.<br>" + "Please enter another email address.";
        url = "/index.jsp";
      } else {
        message = "";
        url = "/thanks.jsp";
        UserDB.insert(user);
      }
      request.setAttribute("user", user);
      request.setAttribute("message", message);
    }
    getServletContext().getRequestDispatcher(url).forward(request, response);
  }
Example #2
0
  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    HttpSession session = request.getSession();

    String url = "/index.jsp";

    // get current action
    String action = request.getParameter("action");
    if (action == null) {
      action = "display_users"; // default action
    }

    // perform action and set URL to appropriate page
    if (action.equals("display_users")) {
      // get list of users
      List<User> users = UserDB.selectUsers();

      // set list as a request attribute
      // forward to index.jsp
    } else if (action.equals("display_user")) {
      // get specified email
      // get user for email
      // set as session attribute
      // forward to user.jsp
    } else if (action.equals("update_user")) {
      // get user from session
      // get new data from request
      // update user
      // update user in database
      // get current list of users
      // set as request attribute
      // forward to index.jsp
    } else if (action.equals("delete_user")) {
      // get the user for the specified email
      // delete the user
      // get current list of users
      // set as request attribute
      // forward to index.jsp
    }

    getServletContext().getRequestDispatcher(url).forward(request, response);
  }