HTTP方法POST在此URL上不受支持的Java Servlet。

huangapple 未分类评论51阅读模式
标题翻译

HTTP method POST is not supported by this URL Java servlet

问题

请问有人可以告诉我为什么我会得到错误:HTTP状态405 - 方法不允许?

我试图在方法doPost()之后实现这样一个功能:用户将被重定向到"/logout"控制器,从而使会话失效。
很有趣,因为方法被调用了,执行了应该执行的所有操作(更新数据库中的用户),但在将错误发送给用户后出现了405错误。另一个我使用doPost()的<form>(例如: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">电子邮件地址</label>
                <input name="email" type="email" class="form-control" id="email"
                       value="${sessionScope.loggedUser.email}" required aria-describedby="emailHelp"
                       placeholder="输入电子邮件">
            </div>
            <div class="form-group">
                <label for="password">密码</label>
                <input name="password" type="password" minlength="5" maxlength="40" required class="form-control"
                       id="password" placeholder="密码">
            </div>
            <div class="form-group">
                <label for="repeatPassword">重复密码</label>
                <input name="repeatPassword" type="password" minlength="5" maxlength="40" required class="form-control"
                       id="repeatPassword" placeholder="密码">
            </div>
            <input class="btn btn-lg btn-primary btn-block" type="submit" value="保存更改"/>
        </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", "密码不相同");
            req.setAttribute("fragment", "account");
            req.getRequestDispatcher("WEB-INF/index.jsp").forward(req, resp);
        }
    }
}

谢谢任何提示。
英文翻译

Can someone please tell me why i am getting error:HTTP Status 405 – Method Not Allowed ?
I am trying accomplish that after method doPost() ,user will be redirected to "/logout" controller ,where is invalidate 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 <form> where i use doPost() (for example: LoginController) working well ,but when i try compere and find bug ,i dont see any :<

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

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

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

        if (password.equals(repeatPassword)) {
            HttpSession session = req.getSession();
            User user = (User) session.getAttribute(&quot;loggedUser&quot;);
            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(&quot;/logout&quot;).forward(req, resp);
        } else {
            req.setAttribute(&quot;errorMessage&quot;, &quot;Passwords not the same&quot;);
            req.setAttribute(&quot;fragment&quot;, &quot;account&quot;);
            req.getRequestDispatcher(&quot;WEB-INF/index.jsp&quot;).forward(req, resp);
        }
    }
}

Thanks for any hint.

答案1

得分: 0

你的doGet()方法调用在服务器的doPost()代码内。你应该重定向响应,doGet用于接收GET请求和任何查询字符串。

英文翻译

Your doGet() method call is inside the server doPost() code. You should redirect the response, doGet is for recieving a GET request and any query string.

答案2

得分: 0

问题已解决。在这里:

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

我应该使用:

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

因为在 "/logout" 中只有一个 doGet() 方法,如果我使用 getRequestDispatcher(),它会尝试查找 doPost() 方法。

英文翻译

Problem solved. Here:

req.getRequestDispatcher(&quot;/logout&quot;).forward(req, resp);

i should do

resp.sendRedirect(req.getContextPath()+&quot;/logout&quot;);

because in "/logout" i have only doGet() method ,and if i use "getRequestDispatcher()" its try to find doPost() method.

huangapple
  • 本文由 发表于 2020年5月30日 18:42:37
  • 转载请务必保留本文链接:https://java.coder-hub.com/62101255.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定