发送表单帖子时无限递归

时间:2016-07-22 14:52:42

标签: servlets recursion model-view-controller

Html表格:

        <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);
    }
}

当我发送表单时,我得到一个无限递归的调用这个servlet和stackoverflow。

哪里有问题?

1 个答案:

答案 0 :(得分:0)

POST请求已经在执行登录servlet,执行forward()只是使用相同的参数再次启动doPost操作并陷入循环。跳过.forward()并只允许servlet重新重新发送登录表单。当用户3次成功登录失败时,可能会将计数器保持在一个只有.forward()到另一个页面。