DispatcherServlet in Spring MVC — Front Controller Pattern
Fast recall
Quick revision
Core answer
Understanding of Spring MVC request processing pipeline and the role of the front controller pattern
Thinking controllers directly handle HTTP requests — DispatcherServlet is the single entry point that orchestrates the entire request flow
Walk through the full request lifecycle: DispatcherServlet → HandlerMapping → Controller → ViewResolver/MessageConverter → Response, mentioning each component's responsibility
Interview-ready guide
Interview answer
For GET /orders/42, the servlet asks its handler mappings for a matching controller method, binds method arguments, invokes the handler, and then either writes the return value through a message converter or renders a view. A servlet filter runs before this flow and can reject the request first. A separately mapped servlet may handle another path. The front-controller idea is central coordination, not a claim of being the first code for every incoming packet.
DispatcherServlet in Spring MVC — Front Controller Pattern
Direct Answer
DispatcherServlet is Spring MVC's front controller servlet. It handles requests mapped to that servlet, finds a handler through HandlerMapping, invokes it through a HandlerAdapter, and coordinates body writing, view rendering, and exception resolution. It does not intercept literally every HTTP request: servlet mappings, filters, static-resource handling, and other servlets determine which requests reach it.
Code Example
@RestController
@RequestMapping("/api/orders")
public class OrderController {
@GetMapping("/{id}")
public Order getOrder(@PathVariable Long id) {
// DispatcherServlet already intercepted this request
// HandlerMapping found this method matches GET /api/orders/{id}
// Now control reaches here
return new Order(id, "Laptop", 1200.0);
// DispatcherServlet will use Jackson to convert Order to JSON
}
}
record Order(Long id, String item, Double price) {}
// Spring Boot auto-configuration equivalent (you don't write this):
// @Bean
// public DispatcherServlet dispatcherServlet() {
// var servlet = new DispatcherServlet();
// servlet.setContextClass(AnnotationConfigWebApplicationContext.class);
// return servlet;
// }
Common Mistakes & Pitfalls
- Thinking controllers are servlets: Freshers often believe each @RestController is a servlet that directly receives HTTP requests. In reality, controllers are plain Spring beans — DispatcherServlet is the only servlet, and it delegates to controllers.
- Not understanding why URL patterns matter: If you manually register DispatcherServlet with pattern
/api/*, requests to/healthwon't reach it. Spring Boot registers it at/so all requests go through it. - Assuming DispatcherServlet does business logic: It's purely a coordinator — it routes requests, invokes handlers, and formats responses, but never contains your domain logic.
Check your understanding
Answer these without looking back at the article.
- What is HandlerMapping in Spring MVC?
- What is the Front Controller design pattern?
- How does Spring Boot auto-configure DispatcherServlet?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.