HttpServletRequest 405错误

这可能是在重写service方法的时候,写了下面的方法
super.service(arg0, arg1);
这会取调用父类的service方法,而父类的service方法会根据请求的是get,还是post类型来调用子类重写的doPost和doGet方法,而我们并没用重写这两个方法所造成的,如此只需要我们在重写service方法的时候去掉这句即可。
这是父类的service方法

    protected void service(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

        String method = req.getMethod();
//如果采用get方式
        if (method.equals(METHOD_GET)) {
            long lastModified = getLastModified(req);
            if (lastModified == -1) {
                // servlet doesn't support if-modified-since, no reason
                // to go through further expensive logic
                doGet(req, resp);
            } else {
                long ifModifiedSince;
                try {
                    ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);
                } catch (IllegalArgumentException iae) {
                    // Invalid date header - proceed as if none was set
                    ifModifiedSince = -1;
                }
                if (ifModifiedSince < (lastModified / 1000 * 1000)) {
                    // If the servlet mod time is later, call doGet()
                    // Round down to the nearest second for a proper compare
                    // A ifModifiedSince of -1 will always be less
                    maybeSetLastModified(resp, lastModified);
                    doGet(req, resp);
                } else {
                    resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                }
            }
//如果采用head方式
        } else if (method.equals(METHOD_HEAD)) {
            long lastModified = getLastModified(req);
            maybeSetLastModified(resp, lastModified);
            doHead(req, resp);
//如果采用post方式
        } else if (method.equals(METHOD_POST)) {
            doPost(req, resp);

        } else if (method.equals(METHOD_PUT)) {
            doPut(req, resp);

        } else if (method.equals(METHOD_DELETE)) {
            doDelete(req, resp);

        } else if (method.equals(METHOD_OPTIONS)) {
            doOptions(req,resp);

        } else if (method.equals(METHOD_TRACE)) {
            doTrace(req,resp);

        } else {
            //
            // Note that this means NO servlet supports whatever
            // method was requested, anywhere on this server.
            //

            String errMsg = lStrings.getString("http.method_not_implemented");
            Object[] errArgs = new Object[1];
            errArgs[0] = method;
            errMsg = MessageFormat.format(errMsg, errArgs);

            resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);
        }
    }

可以看出是根据发送请求的方式来进行的,下面再看父类的doGet方法

 public static final int SC_METHOD_NOT_ALLOWED = 405;
 public static final int SC_BAD_REQUEST = 400;
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
        {
        //获取http版本
            String protocol = req.getProtocol();
            String msg = lStrings.getString("http.method_get_not_supported");
            //如果是1.1版本就发出405错误
            if (protocol.endsWith("1.1")) {
                resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
                //其他版本发出400错误
            } else {
                resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
            }
        }

看看这个神奇的方法
如果调用父类的service方法而不重写它的dopost、doget,他就会调用自身的doget、dopost,然后它自身的dopost、doget会直接发出400、405错误。
就是你不重写就给你好看。
在这里插入图片描述
你以为我说对了吗?
全是错的。
HttpServletRequest中的service是调用了doGet方法,但是它没用写this,其实写不写都一样。
那么这个this指的是什么?
对于普通的父类,this当然指代它自身,但是注意HttpServletRequest 是抽象类,所以它的this指向的是调用它的实例化子类,就是说谁extends了HttpServletRequest他就指向谁,所以指向你自身,它没有被重写,所以要从父类去调用,然后就跟上面一样了。
虽然都是父类发出的400、405错误,但是它是通过子类绕了个弯儿去调用的。

发布了41 篇原创文章 · 获赞 1 · 访问量 4725

猜你喜欢

转载自blog.csdn.net/tomorrow_shoe/article/details/96813959