解决方案:HTTPServlet-此URL不支持Http方法GET

问题出现:

前端页面中表单提交的方法为Post,把servlet代码写在doGet方法内会出现如下的问题:

032_001.jpg

如何解决:

一开始看网上的解决方案是:

1
2
3
4
5
6
7
8
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//业务代码
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req,resp);
}

可是写上之后,仍然会出现405错误

发现此时调用的是父类的方法,应该调用本类中的方法

1
2
3
4
5
6
7
8
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//业务代码
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}

改动之后,问题解决


解决方案:HTTPServlet-此URL不支持Http方法GET
https://yiyuwang.be/2021/03/20/2021-03-20-358597673/
作者
StevenWong
发布于
2021年3月20日
许可协议