Spring - DispatcherServlet
Overview

See “Front Controller” in Core J2EE Patterns. Front Controller design pattern could be implemented by either of following two ways.
- Using Servlet
- Using Filter
DispatcherServlet
is the actually the front controller of Spring MVC. It intercepts each request and then dispatches each to the appropriate controller which has been registered in Spring Application Context.
The default front controller (web server) for spring boot web application is the DispatcherServlet and it creates a new separate thread for every http request. This Servlet then delegates the control flow to the lower layers beginning from controller layer. Typically none of controllers, services and repositories need to hold state and we leave them with default spring-boot bean scope which is singleton.
What happens if you don’t have DispatcherServlet? How will you manage those common functionalities?
We can define a Filter which will intercept each request. The filter will be responsible for handling the common functionalities that are handled by the DispatcherServlet. Struts uses Filter to implement FrontController Design pattern.
Read
- Mediator pattern - refer to “Mediator Pattern in the Spring Framework”
- https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/DispatcherServlet.html
- https://www.oracle.com/java/technologies/front-controller.html
- https://www.baeldung.com/spring-dispatcherservlet