昨天讲解了DispatcherServlet初始化的过程,DispatcherServlet因为继承自HttpServlet,所以其初始化时通过HttpServlet的init方法开始的,整个过程大致是读取ServletContext中的配置参数、然后初始化上下文,初始化Spring mvc需要用到的对象,比如HandlerMapping等。
DispatcherServlet因为是继承自HttpServlet,所以可以通过DispatcherServlet来调用到HttpServlet的API,除了HttpServlet的初始化,DispatcherServlet还可以处理请求,从doGet和doPost方法开始,在接受到前端的get请求时,走doGet方法,在接受到前端的post请求时,走doPost方法。
这两个方法都在DispatcherServlet的父类FrameworkServlet中被重写,调用到processRequest,在processRequest中调用到doService,
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<&