0

Html form:

        <form method="post" action="/login">
            <input class="inputLogin" type="text" name="username" value="" placeholder="Email" >
            <input class="inputPassword" type="password" name="password" value="" placeholder="Password">    
            <br>
            <input class="formButton" type="submit" name="commit" value="Login"> 
        </form>

Servlet:

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String username = req.getParameter(USERNAME_PARAMETER);
    String password = req.getParameter(PASSWORD_PARAMETER);
    boolean authenticated = authService.authenticate(req.getSession(), username, password);
    if (authenticated) {
        resp.sendRedirect("/user/home-page");
    } else {
        System.out.println("ELSE");
        req.setAttribute(ERR_ATTRIBUTE, "Invalid credentials!");
        req.getRequestDispatcher("/login").forward(req, resp);
    }
}

When I send form, I get a infinite recursion of calling this servlet and stackoverflow.

Where is a problem?

clougioc
  • 33
  • 1
  • 5

1 Answers1

0

The POST request is already executing the login servlet, doing a forward() just starts the doPost operation again with the same parameters and gets stuck in a loop. Skip the .forward() and just allow the servlet to resend the login form again. Maybe keep a counter going and only .forward() to another page when the user fails a successful login 3 times.

Mark Olsson
  • 320
  • 2
  • 8