首先,我们看一下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.
理解SpringMvc DispatcherServlet工作流程

本文详细解析了SpringMvc的DispatcherServlet的工作流程,从Servlet继承结构、doService方法到doDispatch的处理,涵盖了异常处理、资源释放、请求处理、拦截器使用、适配器调用等方面,帮助理解DispatcherServlet如何协调请求与响应。
最低0.47元/天 解锁文章
281

被折叠的 条评论
为什么被折叠?



