Exemple #1
0
  /**
   * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
   *
   * @param request servlet request
   * @param response servlet response
   * @throws ServletException if a servlet-specific error occurs
   * @throws IOException if an I/O error occurs
   */
  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    HttpSession session = request.getSession(true);
    try (PrintWriter out = response.getWriter()) {

      String name = request.getParameter("Name");
      String username = request.getParameter("inputUsername");
      String password = request.getParameter("inputPassword");
      String status = null;

      StrongPasswordEncryptor passwordEncryptor = new StrongPasswordEncryptor();
      String encryptedPassword = passwordEncryptor.encryptPassword(password);

      User user = new User(name, username, encryptedPassword, "user", "active");
      UserDAO userDAO = new UserDAO();
      User USER;
      USER = userDAO.getUser(username);

      if (USER != null) {

        session.setAttribute("errorRegister", "Username already taken");
        response.sendRedirect("user/register.jsp");

      } else {

        userDAO.addUser(user);
        response.sendRedirect("index.jsp");
      }
    }
  }
Exemple #2
0
 /**
  * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
  *
  * @param request servlet request
  * @param response servlet response
  * @throws ServletException if a servlet-specific error occurs
  * @throws IOException if an I/O error occurs
  */
 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
     throws ServletException, IOException {
   response.setContentType("text/html;charset=UTF-8");
   try (PrintWriter out = response.getWriter()) {
     // State which url should the data post to
     RequestDispatcher rd = request.getRequestDispatcher("/user/listUser.jsp");
     // DAO is used to fetch data from database
     UserDAO userDAO = new UserDAO();
     // Put the list of data as an attribute to be posted
     request.setAttribute("users", userDAO.getAllUser());
     // Post everything to the web page
     rd.forward(request, response);
   }
 }