欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

servlet:前端向后台发送http请求报错误码405的解决方案。

程序员文章站 2022-07-12 15:48:29
...

项目场景:

在学习Servlet接受前端数据


问题描述:

无论使用post请求还是get请求向后台发送数据,都会报错,错误代码405。

servlet:前端向后台发送http请求报错误码405的解决方案。
接收数据代码:

public class Sign extends HttpServlet {

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        super.service(req, res);

        UserService service = new UserService();
        List<User> users = service.showAllUsers();
        users.forEach(System.out::println);

        res.setContentType("text/html;charset=utf-8");
        PrintWriter writer = res.getWriter();
        users.forEach(val->writer.println(String.format("<h1>%s</h1>", val.toString())));

    }
}

原因分析:

通过代码可以看出,service最终都会访问doPost(),doGet()等其他方法。

protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getMethod();
        long lastModified;
        if (method.equals("GET")) {
            lastModified = this.getLastModified(req);
            if (lastModified == -1L) {
                this.doGet(req, resp);
            } else {
                long ifModifiedSince;
                try {
                    ifModifiedSince = req.getDateHeader("If-Modified-Since");
                } catch (IllegalArgumentException var9) {
                    ifModifiedSince = -1L;
                }

                if (ifModifiedSince < lastModified / 1000L * 1000L) {
                    this.maybeSetLastModified(resp, lastModified);
                    this.doGet(req, resp);
                } else {
                    resp.setStatus(304);
                }
            }
        } else if (method.equals("HEAD")) {
            lastModified = this.getLastModified(req);
            this.maybeSetLastModified(resp, lastModified);
            this.doHead(req, resp);
        } else if (method.equals("POST")) {
            this.doPost(req, resp);
        } else if (method.equals("PUT")) {
            this.doPut(req, resp);
        } else if (method.equals("DELETE")) {
            this.doDelete(req, resp);
        } else if (method.equals("OPTIONS")) {
            this.doOptions(req, resp);
        } else if (method.equals("TRACE")) {
            this.doTrace(req, resp);
        } else {
            String errMsg = lStrings.getString("http.method_not_implemented");
            Object[] errArgs = new Object[]{method};
            errMsg = MessageFormat.format(errMsg, errArgs);
            resp.sendError(501, errMsg);
        }

    }

以doGet()方法为例:

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String msg = lStrings.getString("http.method_get_not_supported");
        this.sendMethodNotAllowed(req, resp, msg); //发送ERROR
    }

这里发送了一个Error,点进来后发现是resp.sendError(405,msg)这个方法在页面上写了错误码405:

    private void sendMethodNotAllowed(HttpServletRequest req, HttpServletResponse resp, String msg) throws IOException {
        String protocol = req.getProtocol();
        if (protocol.length() != 0 && !protocol.endsWith("0.9") && !protocol.endsWith("1.0")) {
            resp.sendError(405, msg);  //最终在页面上显示405
        } else {
            resp.sendError(400, msg);
        }
    }

错误最终原因找到,即调用父类的service()方法时会有可能产生错误码405。


解决方案:

在自定义的servlet类中重写父类service()方法时,不要调用父类的service()方法,即不要写:

super.service(req, res);

本人初识servlet,才疏学浅,如有不当之处,还请各位批评指正。