TomCat中重写HttpServlet的doGet方法的原因

1.Ser

vlet中声明了service方法。

/**
* Servlet 接口
*/
public interface Servlet {
    public void service(ServletRequest req, ServletResponse res);
}

2.GenericServlet 实现了Servlet接口,声明了service方法。

/**
* GenericServlet 实现类,实现了Servlet接口
*/
public abstract class GenericServlet implements Servlet {
    @Override
    public abstract void service(ServletRequest req, ServletResponse res);
}

3.HttpServlet继承了GenericServlet类,重写了service()方法,并将请求传递给了重载的service方法。service方法先判断请求的类型,再执行HttpServlet中对应的请求方法,默认HttpServlet返回给浏览器的结果是一个error,因此HttpServlet要重写对应的请求方法。

/**
* HttpServlet 继承了GenericServlet 类,重载了两个service方法。
* 如果HttpServlet 的子类不重写doget方法,就会调用HttpServlet 内部的doget方法
* 内部的doget方法会响应一个错误给浏览器
*/
public abstract class HttpServlet extends GenericServlet {
	/**
	* HttpServlet 自己的service方法,也是重载的service方法
	*/
  	protected void service(HttpServletRequest req, HttpServletResponse resp){
        String method = req.getMethod();
        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);
                }
            }
        } else if (method.equals(METHOD_POST)) {
            doPost(req, resp);
    }
    /**
    * 从父类GenericServlet 继承下来的service方法
    * 这个方法内部调用了重载的service方法
    */
    @Override
    public void service(ServletRequest req, ServletResponse res)
        throws ServletException, IOException {

        HttpServletRequest  request;
        HttpServletResponse response;

        try {
            request = (HttpServletRequest) req;
            response = (HttpServletResponse) res;
        } catch (ClassCastException e) {
            throw new ServletException(lStrings.getString("http.non_http"));
        }
        service(request, response);
    }
    /**
    * HttpServlet的doGet方法
    * 看源码,发现最后返回给浏览器的结果是一个error
    */
    protected void doGet(HttpServletRequest req, HttpServletResponse resp){
        String msg = lStrings.getString("http.method_get_not_supported");
        sendMethodNotAllowed(req, resp, msg);
    }
    /**
    * doGet调用的方法
    */
    private void sendMethodNotAllowed(HttpServletRequest req, HttpServletResponse resp, String msg) {
        String protocol = req.getProtocol();
        // Note: Tomcat reports "" for HTTP/0.9 although some implementations
        //       may report HTTP/0.9
        if (protocol.length() == 0 || protocol.endsWith("0.9") || protocol.endsWith("1.0")) {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
        } else {
            resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
        }
    }
}