[JAVA] How Dispatcher servlet works in Spring MVC

Role of Spring MVC in Dispatcherservlet

As I said, DispatcherServlet wears many hats in Spring. It acts as a front controller and provides a single entry point for the application. It then uses handler mappings and handler adapters to map a request to the Spring MVC controllers. It uses @Controller and @RequestMapping annotation for that purpose.    Once the request is processed by the Spring MVC controller, it returns a logical view name instead of the view. Though, you can even configure Controler's handler methods to not return any View name by declaring return type as void. You can even use @ResponseBody annotation in the case of REST to directly write the output to the HTTP response body. See REST with Spring course by Eugen to learn more about developing RESTful web services using Spring MVC.    When DispatherServlet receives view name, it consults the ViewResolver to find the right view. There is a chain of ViewResolver is maintained at the Spring MVC framework. They try to resolve the logical view name into a Physical resource e.g. a JSP page or a FreeMaker or Velocity template.    The ViewResolver are invoked in order, if first in the chain not able to resolve the view then it returns null and next ViewResolver in the chain is consults. Once the right view is found, DispatcherServlet forwards the request along with Model data to the View for rendering e.g. a JSP page.      By default, DispatcherServlet uses InternalResourceViewResolver which uses prefix and suffix to convert a logical view name e.g. "home" to /WEB-INF/home.jsp. The View interface also has getContentType() method, which returns content type the view produces (JstlView has text/html). This is usually the default content type for requests handled by the dispatcher servlet in Spring.    Here is a nice diagram which explains how DispatcherServlet works internally in Spring MVC In short, DispatcherServlet is used following things in Spring MVC  - receives all request as Front Controller and provides a single entry point to the application  - mapping requests to correct Spring MVC controller  - Consulting ViewResolvers to find correct View  - forwarding request to chosen View for rendering  - returning the response to the client  - creates web-context to initialize the web specific beans e.g. controllers, view resolvers and handler mapping    That's all about what is the use of DispatcherServlet in Spring framework. It's is one of the key components of Spring MVC which is used to receive all incoming request and forward them to right controllers for actual processing. It finds the right controllers by using handler mappings e.g. SimpleUrlHandlerMapping or BeanNameUrlHandlerMapping, which check if the bean name is the same as view name and the bean implements the View interface.    If you are using annotations then it can also use @Controller and @RequestMapping annotations to find the right controller to process a particular request. Once the request is processed by the controller it returns a logical view name to DispatcherServlet.    The DispatcherServlet then consults ViewResolver and LocalResolvers to find the right View to render the output. Once the correct View is chosen, it forwards the request to the View for rendering the response.

Related blog:

Java concurrency method example

Recommended Posts

How Dispatcher servlet works in Spring MVC
[Java] How Spring DI works
How to use Lombok in Spring
How memory works in object-oriented languages
How to include Spring Tool in Eclipse 4.6.3?
How Spring Security works with Hello World
[Spring MVC] How to pass path variables
SameSite cookie in Spring Boot (Spring Web MVC + Tomcat)
Test controller with Mock MVC in Spring Boot
Deep dive into how HashMap works in Java
Use Servlet filter in Spring Boot [Spring Boot 1.x, 2.x compatible]
MVC in Eclipse.
How to bind to property file in Spring Boot
How Docker works ~ Implement the container in 60 lines
How to define multiple orm.xml in Spring4, JPA2.1
How jul-to-slf4j works
How to create a Spring Boot project in IntelliJ
How to use CommandLineRunner in Spring Batch of Spring Boot
How to test file upload screen in Spring + Selenium
How to use In-Memory Job repository in Spring Batch
Inject Logger in Spring
Spring Boot Servlet mapping
Use Interceptor in Spring
Microservices in Spring Cloud
Get cookies in Spring
How to change application.properties settings at boot time in Spring boot
How to call and use API in Java (Spring Boot)
Enable WebJars for blank projects in TERASOLUNA 5.x (= Spring MVC)
Login with HttpServletRequest # login in Spring Security of Servlet 3.x environment
How to control transactions in Spring Boot without using @Transactional