Scrivio Interview library
InternalsIntermediate6 min read · updated 12 November 2026

How does Spring MVC's DispatcherServlet fit into the servlet lifecycle, and how does Spring Boot register it?

DispatcherServlet is an ordinary servlet with the ordinary lifecycle: its init() creates or attaches to a WebApplicationContext and then initialises its strategies, such as handler mappings, handler adapters, exception resolvers and view resolvers, from that context. Each request arrives through service() and reaches doDispatch(), which finds a handler, runs interceptors, invokes the handler through an adapter, and writes the result through message converters or a view. In Spring Boot, auto-configuration creates the DispatcherServlet bean and a DispatcherServletRegistrationBean that registers it with the embedded container; in a classic war, a WebApplicationInitializer or web.xml registers it.

Why this question is asked

Spring MVC can feel like a separate world from the Servlet API, but it is built directly on it, and knowing exactly where the two meet explains a lot: why filters run before interceptors, why the first request is sometimes slow, and how Boot can start Tomcat without any web.xml. Interviewers ask this internals question to see whether we understand that connection. In this answer, we will recap the servlet lifecycle, follow DispatcherServlet's initialisation, follow one request through doDispatch, and then see how Spring Boot and a classic war each register it.

1A Quick Recap: DispatcherServlet Is Just a Servlet

A servlet has three lifecycle calls made by the container: init() once, service() for every request, and destroy() once at shutdown. DispatcherServlet extends FrameworkServlet, which extends HttpServletBean, which extends the standard HttpServlet. So the container treats it exactly like any servlet we write ourselves.

Its special role is that it is a front controller: one servlet, usually mapped to /, receives every request for the application and dispatches it to our controllers. All the Spring MVC features we use, @GetMapping, @RequestBody, validation, exception handlers, happen inside its service() call.

2What init() Does

When the container calls init(), HttpServletBean copies the servlet's init parameters onto the servlet's properties, and FrameworkServlet's initServletBean() sets up the WebApplicationContext, the Spring container that holds the web beans. In a classic setup, FrameworkServlet creates its own child context from a configuration class or XML file, with the root context created by ContextLoaderListener as its parent. In Spring Boot, the DispatcherServlet is itself a bean in the application context, so it is given that existing context and uses it instead of creating a new one.

Once the context is ready, DispatcherServlet calls initStrategies(), which looks up its helper objects in the context, or falls back to defaults: HandlerMappings that map URLs to handlers, such as RequestMappingHandlerMapping for @RequestMapping methods; HandlerAdapters that know how to invoke each kind of handler; HandlerExceptionResolvers for @ExceptionHandler and error statuses; ViewResolvers; the LocaleResolver; and a few more. After that, the servlet is ready for requests.

3Following One Request Through doDispatch

For each request, the container calls service(). FrameworkServlet routes every HTTP method to processRequest(), which sets up request-scoped context such as the locale, and calls doService(), which stores a few attributes and calls doDispatch(). doDispatch is the heart of Spring MVC.

Java
// A simplified view of DispatcherServlet.doDispatch (not the real source)
HandlerExecutionChain chain = getHandler(request); // OrderController#get, plus its interceptors
HandlerAdapter adapter = getHandlerAdapter(chain.getHandler()); // RequestMappingHandlerAdapter
if (!chain.applyPreHandle(request, response)) return; // an interceptor said stop
ModelAndView mv = adapter.handle(request, response, chain.getHandler());
// resolves @PathVariable and @RequestBody, calls the method,
// and for @ResponseBody writes JSON through HttpMessageConverters (mv is null)
chain.applyPostHandle(request, response, mv);
processDispatchResult(request, response, chain, mv, exception); // render a view or resolve an exception
// finally: chain.triggerAfterCompletion(request, response, exception)
Step throughOne request through DispatcherServlet
ContainerDispatcherServletHandlerMappingInterceptorController
service(request, response)
processRequest -> doService -> doDispatch
getHandler(request)
handler + interceptors
preHandle
invoke via HandlerAdapter
return value
write body with message converter, or render view
postHandle, afterCompletion

9 messages in order. Play them, or step through them one at a time.

Filters run in the container before service(); interceptors run inside doDispatch.

Notice the order. The handler mappings find which controller method matches the URL and which interceptors apply. The interceptors' preHandle methods run before the controller, and any of them can stop the request. The adapter invokes the controller, converting arguments and the return value; for a REST controller, message converters such as the Jackson one write the JSON body. Then postHandle runs, a view is rendered or an exception is resolved, and afterCompletion always runs. Filters are not in this picture at all, because they run in the container before service() is even called.

4How Spring Boot Registers It

In Spring Boot there is no web.xml. When the application starts, DispatcherServletAutoConfiguration creates a DispatcherServlet bean and a DispatcherServletRegistrationBean. The registration bean is a ServletRegistrationBean, a Spring Boot class that implements ServletContextInitializer, and it records the servlet, its URL mapping and its load-on-startup value.

Spring Boot then starts the embedded container itself. ServletWebServerApplicationContext asks a web server factory, for example the Tomcat one, to create the server, and as the servlet context starts, Boot calls every ServletContextInitializer bean. The DispatcherServletRegistrationBean adds the DispatcherServlet to the servlet context, mapped to /, and the filter registration beans add the filters. Because this happens through beans, we can customise it with properties.

Java
# application.properties
spring.mvc.servlet.path=/api # DispatcherServlet mapped to /api/* instead of /
spring.mvc.servlet.load-on-startup=1 # init at startup; the default -1 means lazily, on the first request

The second property explains a familiar symptom: by default, the first request after startup logs Initializing Spring DispatcherServlet 'dispatcherServlet' and takes a little longer, because the servlet's init() runs lazily then.

5How a Classic War Registers It

In a war deployed to an external Tomcat, someone else starts the container, so the application must register the servlet. The old way is a servlet entry in web.xml. Since Servlet 3.0, Spring uses the ServletContainerInitializer mechanism instead: the container finds Spring's SpringServletContainerInitializer through a service file in the spring-web jar, and that class calls every WebApplicationInitializer implementation in the application.

Java
public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override protected Class<?>[] getRootConfigClasses() { return new Class<?>[] { RootConfig.class }; }
@Override protected Class<?>[] getServletConfigClasses() { return new Class<?>[] { WebConfig.class }; }
@Override protected String[] getServletMappings() { return new String[] { "/" }; }
}
// registers ContextLoaderListener for the root context and a DispatcherServlet mapped to /

A Spring Boot application packaged as a war uses SpringBootServletInitializer, which is itself a WebApplicationInitializer, to start the Boot application inside the external container, and from there the same auto-configuration registers DispatcherServlet.

6Follow-up questions

Can there be more than one DispatcherServlet?

Yes. Each is a separate servlet with its own mapping and, in classic setups, its own child WebApplicationContext. In Spring Boot we can register another one with our own ServletRegistrationBean, for example for a legacy path. It is rarely needed today, because one DispatcherServlet with several controllers covers most cases.

Where do message converters come from in Boot?

HttpMessageConvertersAutoConfiguration and the MVC auto-configuration register them, including a Jackson converter when Jackson is on the classpath, and RequestMappingHandlerAdapter uses them to read @RequestBody and write @ResponseBody values.

What runs before DispatcherServlet?

The container's filter chain: every registered servlet filter, including Spring Security's filter, runs before service() is called on DispatcherServlet. That is why a filter can reject a request before any controller or interceptor sees it.

7Conclusion

In this answer, we saw that DispatcherServlet is a normal servlet. Its init() sets up or receives a WebApplicationContext and initialises handler mappings, adapters, resolvers and converters from it; each request flows through service() into doDispatch(), which finds a handler, runs interceptors, invokes the handler through an adapter and writes the result through message converters or a view. Spring Boot registers it through auto-configuration and a DispatcherServletRegistrationBean on the embedded container, while a classic war uses a WebApplicationInitializer or web.xml.

What to say in the interviewAbout 90 seconds aloud

Open with

DispatcherServlet is an ordinary servlet: it extends FrameworkServlet and ultimately HttpServlet, so the container calls init once, service per request and destroy at shutdown. In init, FrameworkServlet sets up the WebApplicationContext.

Then, if they want more

In a classic setup it creates a child context with the root context from ContextLoaderListener as parent; in Spring Boot the servlet is itself a bean, so it just uses the existing application context. Then initStrategies looks up the handler mappings, handler adapters, exception resolvers, view resolvers and so on from the context.

For each request, service goes through processRequest and doService to doDispatch. doDispatch asks the handler mappings for a HandlerExecutionChain, the controller method plus its interceptors, gets the matching HandlerAdapter, runs the interceptors' preHandle, invokes the controller through the adapter, which resolves arguments and, for REST controllers, writes the response body with message converters like Jackson, then runs postHandle, renders a view or resolves an exception, and always calls afterCompletion.

Filters run before all of this, in the container. In Spring Boot, DispatcherServletAutoConfiguration creates the DispatcherServlet bean and a DispatcherServletRegistrationBean, which is a ServletContextInitializer; when Boot starts the embedded Tomcat, it calls those initializers and the servlet is added, mapped to / by default and initialised lazily unless spring.mvc.servlet.load-on-startup is set. In a classic war, web.xml or a WebApplicationInitializer, usually AbstractAnnotationConfigDispatcherServletInitializer, registers it.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Jakarta EE specifications
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How does Spring MVC's DispatcherServlet fit into the servlet lifecycle, and how does...": https://scrivio-interview-library.netlify.app/java/servlets/ae8e9143dc/