简体   繁体   English

此 URL Java servlet 不支持 HTTP 方法 POST

[英]HTTP method POST is not supported by this URL Java servlet

Can someone please tell me why i am getting error:HTTP Status 405 – Method Not Allowed?有人可以告诉我为什么我收到错误:HTTP 状态 405 – 方法不允许? I am trying accomplish that after method doPost(),user will be redirected to "/logout" controller,where is invalidate session.我正在尝试实现在方法 doPost() 之后,用户将被重定向到“/logout”controller,其中 session 无效。 It's funny because method is called,do everything what should do(update user in database), but after send to user error 405.Another where i use doPost() (for example: LoginController) working well,but when i try compere and find bug,i dont see any:<这很有趣,因为调用了方法,做所有应该做的事情(更新数据库中的用户),但是在发送给用户错误 405 之后。我使用 doPost() 的另一个地方(例如:LoginController)运行良好,但是当我尝试主持人并找到错误,我没有看到任何:<

<div class="container">
    <div class="col-md-8 col-md-offset-2">
        <form method="post" action="account">
            <div class="form-group">
                <label for="email">Email address</label>
                <input name="email" type="email" class="form-control" id="email"
                       value="${sessionScope.loggedUser.email}" required aria-describedby="emailHelp"
                       placeholder="Enter email">
            </div>
            <div class="form-group">
                <label for="password">Password</label>
                <input name="password" type="password" minlength="5" maxlength="40" required class="form-control"
                       id="password" placeholder="Password">
            </div>
            <div class="form-group">
                <label for="repeatPassword">Repeat Password</label>
                <input name="repeatPassword" type="password" minlength="5" maxlength="40" required class="form-control"
                       id="repeatPassword" placeholder="Password">
            </div>
            <input class="btn btn-lg btn-primary btn-block" type="submit" value="Save changes"/>
        </form>
    </div>
</div>

@WebServlet("/account")
public class AccountController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String email = req.getParameter("email");
        String password = req.getParameter("password");
        String repeatPassword = req.getParameter("repeatPassword");

        if (email == null || password == null || repeatPassword == null) {
            doGet(req, resp);
            return;
        }

        if (password.equals(repeatPassword)) {
            HttpSession session = req.getSession();
            User user = (User) session.getAttribute("loggedUser");
            user.setEmail(email);
            String sha1hexPassword = DigestUtils.sha1Hex(password);
            user.setPassword(sha1hexPassword);
            UserService service = new UserService();

            try {
                service.update(user);
            } catch (UpdateObjectException e) {
                e.printStackTrace();
            }
            req.getRequestDispatcher("/logout").forward(req, resp);
        } else {
            req.setAttribute("errorMessage", "Passwords not the same");
            req.setAttribute("fragment", "account");
            req.getRequestDispatcher("WEB-INF/index.jsp").forward(req, resp);
        }
    }
}

Thanks for any hint.感谢您的任何提示。

Your doGet() method call is inside the server doPost() code.您的 doGet() 方法调用位于服务器 doPost() 代码中。 You should redirect the response, doGet is for recieving a GET request and any query string.您应该重定向响应,doGet 用于接收 GET 请求和任何查询字符串。

Problem solved.问题解决了。 Here:这里:

req.getRequestDispatcher("/logout").forward(req, resp);

i should do我应该做

resp.sendRedirect(req.getContextPath()+"/logout");

because in "/logout" i have only doGet() method,and if i use "getRequestDispatcher()" its try to find doPost() method.因为在“/logout”中我只有doGet()方法,如果我使用“getRequestDispatcher()”,它会尝试找到doPost()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM