JavaWeb—HTTP状态 405 - 方法不允许【BUG已解决】
发布时间:2022-09-21 22:42:36 451
相关标签: # 前端# java# 后端# java
一.遇到的问题
这是一个JavaWeb工程,写了请求表单之后报了HTTP状态 405 - 方法不允许。此URL不支持Http方法POST。如下图:
我目前是写了应该表单,是使用了post方法,get可以正常请求,post就会405。
我的前端代码如下,没有错误:
错误在后端,请看下文。
二.此URL不支持Http方法POST【解决方法】
后端我继承了HttpServlet,重写了doGet。如下
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setCharacterEncoding("utf-8");
String user = req.getParameter("username");
String pass = req.getParameter("password");
String[] yus = req.getParameterValues("yu");
System.out.println("---------------------");
System.out.println(user);
System.out.println(pass);
System.out.println(Arrays.toString(yus));
System.out.println("---------------------");
req.getRequestDispatcher("/sussess.jsp").forward(req,resp);
}
为了解决这个HTTP状态 405 - 方法不允许的bug,我们需要重写一下doPost方法: 让doPost直接指向doGet,即可解决这个问题。 代码如下:
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
HTTP状态 405 - 方法不允许/此URL不支持Http方法POST——BUG已解决。
文章来源: https://blog.51cto.com/u_15568258/5627031
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报