gpt4 book ai didi

java - 此 URL Java servlet 不支持 HTTP 方法 POST

转载 作者:行者123 更新时间:2023-12-01 16:28:36 24 4
gpt4 key购买 nike

有人可以告诉我为什么我收到错误:HTTP 状态 405 – 不允许方法吗?我正在尝试完成 doPost() 方法之后,用户将被重定向到“/logout” Controller ,其中 session 无效。这很有趣,因为方法被调用,做了所有应该做的事情(更新数据库中的用户),但是在发送给用户错误405之后。另一个我使用 doPost() (例如:LoginController)工作良好的地方,但是当我尝试 compere 并发现错误,我没有看到任何错误:<

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

感谢您的任何提示。

最佳答案

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

关于java - 此 URL Java servlet 不支持 HTTP 方法 POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62101255/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com