If you lot accept worked alongside Spring MVC together with thus you lot should know what is a DispatcherServlet? It's genuinely the pump of Spring MVC, exactly the C of MVC blueprint pattern or Controller. Every unmarried spider web asking which is supposed to live processed past times Spring MVC goes through DispatcherServlet. In general, its an implementation of Front Controller Pattern which provides a unmarried betoken of entry inwards your application. It handles all incoming requests. It is likewise the duo betwixt Java together with Spring. Btw, the DispatcherServlet is similar whatever other Servlet is declared inwards the web.xml alongside a URL pattern only the entirely exceptional affair is that the URL pattern for dispatcher servlet is plenty to map every unmarried spider web asking to DispathcherServlert.
It is responsible for asking treatment past times delegating requests to additional components of Spring MVC e.g. actual controller classes i.e. those which are annotated using @Controller or @RestController (in instance of RESTful Web Services), Views, View Resolvers, handler mappers etc.
Though the undertaking of actual asking mapping is done past times @RequestMapping annotation, it's genuinely the DispatcherServlet which delegates asking to the corresponding controller.
In the instance of RESTful spider web services, it is likewise responsible for finding the right message converter to convert the response into the format customer is expecting e.g. JSON, XML, or TEXT.
For example, if a customer is expecting JSON together with thus it volition work the MappingJacksonHttpMessageConverter or MappingJackson2HttpMessageConverter (depending upon whether Jackson 1 or Jackson ii is available inwards Classpath) to convert the response returned past times convert into a JSON string.
You tin terminate farther see Spring Framework 5: Beginner to Guru to acquire to a greater extent than close developing RESTful spider web services using Spring four together with Spring 5.
The DispatcherServlet uses HandlerMapping implementations - pre-built or provided every bit run of the application to road incoming requests to handler objects. By default, it uses BeanNameUrlHandlerMapping together with DefaultAnnotationHandlerMapping, which is driven by @RequestMapping annotation.
In lodge to honour the right methods for treatment the request, it scans through all the classes declared using @Controller notation together with it likewise uses @RequestMapping notation to honour the types together with methods responsible for treatment requests. @RequestMapping notation tin terminate map the asking past times path e.g. @RequestMapping(“path”), by HTTP method e.g. @RequestMapping("path", method=RequestMethod.GET), past times asking parameters e.g. @RequestMapping("path"”, method=RequestMethod.POST, params="param1") together with past times presence of HTTP asking header like
@RequestMapping("path", header="content-type=text/*").
You tin terminate likewise apply @RequestMapping notation at the class score to filter incoming requests. If you lot are interested, you lot tin terminate farther check Spring Framework 5: Beginner to Guru course on Udemy to acquire the amount attain of options you lot tin terminate work alongside the @RequestMapping annotation. I'll likewise write an article close that only till together with thus this class is 1 of the best resources.
Anyway, afterward processing the asking Controller returns logical sentiment cry together with model to DispatcherServlet. It together with thus consults to sentiment resolvers to honour the actual View to homecoming the output.
The sentiment resolution strategy tin terminate live specified using a ViewResolver implementation, past times default, DispatcherServlet uses InternalResourceViewResolver to convert logical sentiment cry to actual View object e.g. a JSP.
After this DispatcherServlet contacts the chosen sentiment e.g. a JSP file alongside model information together with it renders the output depending on the model data. This rendered output is returned to the customer every bit a response. Sometimes you lot don't fifty-fifty demand a sentiment e.g. inwards instance of RESTful Web services.
Their handler method straight writes into response using @ResponseBody notation together with DispatcherServlet straight homecoming a response to the client. See REST alongside Spring course to acquire to a greater extent than close developing together with testing RESTful Web service using Spring MVC.
In this article, I am going to part or thus of the useful together with essential things close DispatcherServlet a Java Web Developer should know. These points volition non entirely assist you lot to empathise the undertaking of DispatcherServlet inwards Spring MVC improve only likewise encourage you lot to acquire to a greater extent than close them past times exploring together with researching each point.
1) The DispatcherServlet is the chief controller of Spring MVC Application. All incoming spider web asking passes through DispatcherServlet earlier processed past times private Spring controllers i.e classes annotated using @Controller annotation.
2) The DispatcherServlet of Spring MVC is an Implementation of Front Controller Pattern (see how Spring MVC plant internally for to a greater extent than details of the inner workings of Spring.
6) Spring's DispatcherServlet is likewise completely integrated alongside the Spring IoC container together with every bit such allows you lot to work every characteristic of Spring framework e.g. dependency injection.
7) The dispatcher servlet is configured every bit load-on-startup = 1 which agency this Servlet should live created past times Servlet container when you lot deploy the application rather than creating when a asking arrived for this request.
This is done to bring down the response fourth dimension of the starting fourth dimension asking because DispatcherServlet does a lot of undertaking at the startup to scan together with honour all controllers together with asking mappings. See
It is responsible for asking treatment past times delegating requests to additional components of Spring MVC e.g. actual controller classes i.e. those which are annotated using @Controller or @RestController (in instance of RESTful Web Services), Views, View Resolvers, handler mappers etc.
Though the undertaking of actual asking mapping is done past times @RequestMapping annotation, it's genuinely the DispatcherServlet which delegates asking to the corresponding controller.
In the instance of RESTful spider web services, it is likewise responsible for finding the right message converter to convert the response into the format customer is expecting e.g. JSON, XML, or TEXT.
For example, if a customer is expecting JSON together with thus it volition work the MappingJacksonHttpMessageConverter or MappingJackson2HttpMessageConverter (depending upon whether Jackson 1 or Jackson ii is available inwards Classpath) to convert the response returned past times convert into a JSON string.
You tin terminate farther see Spring Framework 5: Beginner to Guru to acquire to a greater extent than close developing RESTful spider web services using Spring four together with Spring 5.
How does DispatcherServlet procedure asking inwards Spring MVC
As I said before, Dispatcher Servlet is used to possess all incoming requests together with road them through unlike Spring Controllers for farther processing. To attain this, it determines which controllers should possess the incoming request.The DispatcherServlet uses HandlerMapping implementations - pre-built or provided every bit run of the application to road incoming requests to handler objects. By default, it uses BeanNameUrlHandlerMapping together with DefaultAnnotationHandlerMapping, which is driven by @RequestMapping annotation.
In lodge to honour the right methods for treatment the request, it scans through all the classes declared using @Controller notation together with it likewise uses @RequestMapping notation to honour the types together with methods responsible for treatment requests. @RequestMapping notation tin terminate map the asking past times path e.g. @RequestMapping(“path”), by HTTP method e.g. @RequestMapping("path", method=RequestMethod.GET), past times asking parameters e.g. @RequestMapping("path"”, method=RequestMethod.POST, params="param1") together with past times presence of HTTP asking header like
@RequestMapping("path", header="content-type=text/*").
You tin terminate likewise apply @RequestMapping notation at the class score to filter incoming requests. If you lot are interested, you lot tin terminate farther check Spring Framework 5: Beginner to Guru course on Udemy to acquire the amount attain of options you lot tin terminate work alongside the @RequestMapping annotation. I'll likewise write an article close that only till together with thus this class is 1 of the best resources.
Anyway, afterward processing the asking Controller returns logical sentiment cry together with model to DispatcherServlet. It together with thus consults to sentiment resolvers to honour the actual View to homecoming the output.
The sentiment resolution strategy tin terminate live specified using a ViewResolver implementation, past times default, DispatcherServlet uses InternalResourceViewResolver to convert logical sentiment cry to actual View object e.g. a JSP.
After this DispatcherServlet contacts the chosen sentiment e.g. a JSP file alongside model information together with it renders the output depending on the model data. This rendered output is returned to the customer every bit a response. Sometimes you lot don't fifty-fifty demand a sentiment e.g. inwards instance of RESTful Web services.
Their handler method straight writes into response using @ResponseBody notation together with DispatcherServlet straight homecoming a response to the client. See REST alongside Spring course to acquire to a greater extent than close developing together with testing RESTful Web service using Spring MVC.
10 Points close DispatcherServlet
In this article, I am going to part or thus of the useful together with essential things close DispatcherServlet a Java Web Developer should know. These points volition non entirely assist you lot to empathise the undertaking of DispatcherServlet inwards Spring MVC improve only likewise encourage you lot to acquire to a greater extent than close them past times exploring together with researching each point.1) The DispatcherServlet is the chief controller of Spring MVC Application. All incoming spider web asking passes through DispatcherServlet earlier processed past times private Spring controllers i.e classes annotated using @Controller annotation.
2) The DispatcherServlet of Spring MVC is an Implementation of Front Controller Pattern (see how Spring MVC plant internally for to a greater extent than details of the inner workings of Spring.
6) Spring's DispatcherServlet is likewise completely integrated alongside the Spring IoC container together with every bit such allows you lot to work every characteristic of Spring framework e.g. dependency injection.
7) The dispatcher servlet is configured every bit load-on-startup = 1 which agency this Servlet should live created past times Servlet container when you lot deploy the application rather than creating when a asking arrived for this request.
This is done to bring down the response fourth dimension of the starting fourth dimension asking because DispatcherServlet does a lot of undertaking at the startup to scan together with honour all controllers together with asking mappings. See
9) In the Spring MVC framework, each DispatcherServlet has its ain WebApplicationContext (see Spring inwards Action), which inherits all the beans already defined inwards the root WebApplicationContext. These inherited beans tin terminate live overridden inwards the servlet-specific scope, together with novel scope-specific beans tin terminate live defined locally to a given servlet instance.
10) The DispatcherServlet of Spring MVC framework tin terminate likewise homecoming the last-modification-date, every bit specified past times the Servlet API. It determines the terminal alteration appointment past times looking for an appropriate handler mapping together with examine if the handler that is institute implements the LastModified interface. If yes, together with thus it calls the getLastModified(request) method of the LastModified interface together with value is returned to the client.
That's all close DispatcherServlet of Spring MVC framework. As I said, the DispatcherServlet is the backbone of Spring MVC together with serves every bit the chief controller which routes unlike HTTP asking to corresponding Spring Controllers. It is an implementation of Front Controller blueprint pattern together with provides a unmarried entry betoken to your Spring spider web application.
You configure DispatcherServlet inwards the web.xml only gives it load-on-startup value 1 to advise container to charge this Servlet during startup together with non when a asking comes up. This reduces response fourth dimension for the starting fourth dimension request.
Further Learning
Spring Master Class - Beginner to Expert
Spring Framework 5: Beginner to Guru
REST alongside Spring past times Baeldung
Other Spring related articles you lot may similar to explore
- Difference between @RestControler together with @Controller inwards Spring MVC? (answer)
- 23 Spring MVC Interview questions for ii to iii years experienced (list)
- What is the work of DispatcherServlet inwards Spring MVC? (answer)
- How to enable Spring safety inwards Java application? (answer)
- Does Spring certification assist inwards Job together with Career? (article)
- Top five Spring Certification Mock Exams (list)
- Spring Framework MasterClass past times Udemy (see)
- Difference betwixt @Autowired together with @Injection annotations inwards Spring? (answer)
- 5 Spring together with Hibernate online courses for Java developers (list)
Thanks for reading this article thus far. If you lot similar this article together with thus delight part alongside your friends together with colleagues. If you lot accept whatever question, suggestion, or feedback or you lot accept whatever betoken close dispatcher servlet which is non mentioned hither only worth knowing together with thus delight drib a comment together with I'll include it inwards the chief article.
P. S. - If you lot don't heed learning from gratis resources together with thus you lot tin terminate likewise cheque out my listing of Free Spring Core together with Spring Boot courses for Java developers to start your journey.