首先,我们看一下DispatcherServlet的继承关系图。这里,我们着重看一下Servlet部分的继承树。下图中我们可以看到,DispatcherServlet就是实现了Servlet接口。而用来处理请求的service方法在其父类FrameworkServlet中进行了重写。
其次,咱们看一下doService方法:在此方法中,除了定义了请求的属性及对应的操作外,主要是调用了doDispatch方法来处理请求的。
@Override protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception { logRequest(request); // Keep a snapshot of the request attributes in case of an include, // to be able to restore the original attributes after the include. Map<String, Object> attributesSnapshot = null; if (WebUtils.isIncludeRequest(request)) { attributesSnapshot = new HashMap<>(); Enumeration<?> attrNames = request.getAttributeNames(); while (attrNames.hasMoreElements()) { String attrName = (String) attrNames.nextElement(); if (this.cleanupAfterInclude || attrName.startsWith(DEFAULT_STRATEGIES_PREFIX)) { attributesSnapshot.put(attrName, request.getAttribute(attrName)); } } } // Make framework objects available to handlers and view objects. request