Servlets and listeners are the two low-level building blocks that bootstrap and drive a Spring MVC web application inside a servlet container, sitting beneath the higher-level annotations most developers work with day to day.
Key Points: • DispatcherServlet is the front controller — every HTTP request enters through it and gets routed to the matching handler method. • ContextLoaderListener implements ServletContextListener and creates the root application context when the web app starts up. • The root context typically holds shared beans (services, repositories), while DispatcherServlet owns a child context scoped to web-layer beans (controllers, view resolvers). • Listeners can also hook into session and context lifecycle events, useful for cleanup or initialization logic. • In Java-based configuration, AbstractAnnotationConfigDispatcherServletInitializer registers both the servlet and the listener behavior without needing web.xml.
Example: When Tomcat starts a Spring MVC app, ContextLoaderListener fires first and builds the root context with your service and repository beans; DispatcherServlet then starts, builds its own web context on top of that root, and begins accepting requests at the configured URL pattern, typically "/".
Interview Tip: A concise interview answer is:
"Servlets and listeners are what actually wire a Spring MVC app into the servlet container. ContextLoaderListener starts up first and builds the root application context with shared beans, and DispatcherServlet then starts as the front controller, building a child web context and routing every incoming request to the right handler."