DispatcherServlet in Spring MVC
DispatcherServlet is the central entry point for request processing in Spring MVC. It coordinates finding the appropriate controller, invoking it, and producing the HTTP response.
DispatcherServlet implements the Front Controller pattern: incoming requests are routed through one central servlet rather than being handled directly by many independent servlets. In a typical Spring MVC application, it receives an HTTP request and manages the processing workflow.
The servlet uses handler mappings to determine which controller method should handle the request. The selected handler is invoked through the appropriate handler adapter, allowing Spring MVC to support different handler types and controller styles.
After the controller returns, DispatcherServlet helps determine how the result becomes a response. Depending on the controller and configuration, this can involve resolving a view and rendering it, or writing data directly to the response, such as JSON produced by `@RestController` methods.
This centralization also gives Spring MVC a consistent place to apply concerns such as exception handling and request/response processing. For a junior interview, remember that DispatcherServlet coordinates the workflow rather than containing the business logic itself.
Key points
- Acts as Spring MVC's Front Controller.
- Receives HTTP requests and coordinates their processing.
- Uses handler mappings to find the appropriate controller handler.
- Uses handler adapters to invoke the selected handler.
- Coordinates view rendering or direct response-body handling.
Interview questions
What is DispatcherServlet in Spring MVC?
DispatcherServlet is the central servlet used by Spring MVC to process incoming web requests. It follows the Front Controller pattern and coordinates the request-processing workflow. It finds an appropriate handler, invokes it, and helps produce the HTTP response. It is not where application business logic normally belongs.
How does DispatcherServlet find the controller method for a request?
DispatcherServlet delegates handler selection to handler-mapping infrastructure. The mapping examines information such as the request path and HTTP method and identifies a suitable handler. For annotation-based controllers, mappings such as `@GetMapping` and `@PostMapping` contribute to this selection. DispatcherServlet then uses a handler adapter to invoke the selected handler.
What happens after a controller method returns?
The next step depends on how the controller is designed. A traditional MVC controller can return information used to resolve and render a view. A REST controller commonly returns data that Spring writes to the HTTP response body through message conversion. DispatcherServlet coordinates these steps rather than deciding the application's business result itself.
Why is DispatcherServlet called a Front Controller?
The Front Controller pattern places a central component in front of application request handlers. DispatcherServlet fills that role in Spring MVC. Requests enter through this central servlet, which coordinates routing and other common processing before and after the controller executes. This gives the framework a consistent place for cross-cutting request concerns.
Is DispatcherServlet responsible for business logic?
No. DispatcherServlet is framework infrastructure responsible for coordinating web request processing. Business logic should normally live in application services or other domain-oriented components. Controllers should generally translate HTTP-level input into calls to that application logic. Keeping these responsibilities separate improves maintainability and testability.