Thursday, April 25, 2024
HomeJavaWhat's the usage of DispatcherServlet in Spring MVC Framework?

What’s the usage of DispatcherServlet in Spring MVC Framework?


If in case you have labored with Spring MVC then you must know what’s a DispatcherServlet? It is truly the guts of Spring MVC, exactly the C of MVC design sample or Controller. Each single internet request which is meant to be processed by Spring MVC goes by way of DispatcherServlet. Basically, its an implementation of Entrance Controller Sample which offers a single level of entry in your software. It handles all incoming requests. It’s also the bridge between Java and Spring. Btw, the DispatcherServlet is like every other Servlet is said within the internet.xml with a URL sample however the one particular factor is that the URL sample for dispatcher servlet is sufficient to map each single internet request to DispathcherServlert.

It’s answerable for request dealing with by delegating requests to further elements of Spring MVC e.g. precise controller lessons i.e. these that are annotated utilizing
@Controller or @RestController (in case of RESTful Internet Companies), Views, View Resolvers, handler mappers, and many others.

Although the job of precise request mapping is completed by @RequestMapping annotation, it is truly the DispatcherServlet which delegates request to the corresponding controller.

Within the case of RESTful internet providers, additionally it is answerable for discovering the proper message converter to transform the response into the format shopper is anticipating like JSON, XML, or TEXT.

For instance, if a shopper is anticipating JSON then it is going to use the MappingJacksonHttpMessageConverter or MappingJackson2HttpMessageConverter (relying upon whether or not Jackson 1 or Jackson 2 is offered in Classpath) to transform the response returned by convert right into a JSON string.

You may additional see Spring Framework 5: Newbie to Guru to be taught extra about creating RESTful internet providers utilizing Spring 4 and Spring 5.

How does DispatcherServlet course of request in Spring MVC

As I stated earlier than, Dispatcher Servlet is used to deal with all incoming requests s and route them by way of totally different Spring Controllers for additional processing. To realize this, it determines which controllers ought to deal with the incoming request.

The DispatcherServlet makes use of HandlerMapping implementations – pre-built or supplied as a part of the appliance to route incoming requests to handler objects. By default, it makes use of BeanNameUrlHandlerMapping and DefaultAnnotationHandlerMapping, which is pushed by @RequestMapping annotation.

As a way to discover the proper strategies for dealing with the request, it scans by way of all of the lessons declared utilizing @Controller annotation and it additionally makes use of @RequestMapping annotation to search out the kinds and strategies answerable for dealing with requests.

The @RequestMapping annotation can map the request

  1. by path like @RequestMapping(“path”), 
  2. by HTTP methodology like @RequestMapping(“path”, methodology=RequestMethod.GET)
  3. by request parameters like @RequestMapping(“path””, methodology=RequestMethod.POST, params=”param1”)
  4. and by the presence of HTTP request header like @RequestMapping(“path”, header=”content-type=textual content/*”).

You may as well apply @RequestMapping annotation on the class stage to filter incoming requests. If you’re , you’ll be able to additional examine Spring Framework 5: Newbie to Guru course on Udemy to be taught the total vary of choices you should use with the @RequestMapping annotation. I will additionally write an article about that however until then this course is without doubt one of the greatest assets.

What is the Use of DispatcherServlet in Spring MVC Framework?

Anyway, after processing the request Controller returns the logical view title and mannequin to DispatcherServlet. It then consults to view resolvers to search out the precise View to render the output.

The view decision technique may be specified utilizing a ViewResolver implementation, by default, DispatcherServlet makes use of InternalResourceViewResolver to transform logical view title to precise View object like a JSP.

After this DispatcherServlet contacts the chosen view e.g. a JSP file with mannequin knowledge and it renders the output relying on the mannequin knowledge. This rendered output is returned to the shopper as a response. Generally you do not even want a view e.g. in case of RESTful Internet providers.

Their handler methodology immediately writes into response utilizing @ResponseBody annotation and DispatcherServlet immediately returns a response to the shopper. See REST with Spring course to be taught extra about creating and testing RESTful Internet service utilizing Spring MVC.

what is the use of DispatcherServlet in Spring 5


10 Factors about DispatcherServlet

On this article, I’m going to share a few of the helpful and important issues about DispatcherServlet a Java Internet Developer ought to know. These factors won’t solely show you how to to know the job of DispatcherServlet in Spring MVC higher but additionally encourage you to be taught extra about them by exploring and researching every level.

1) The DispatcherServlet is the primary controller of the Spring MVC Utility. All incoming internet request passes by way of DispatcherServlet earlier than processed by particular person Spring controllers i.e lessons annotated utilizing @Controller annotation.

2) The DispatcherServlet of Spring MVC is an Implementation of Entrance Controller Sample (see Introduction to Spring MVC 4). A Entrance Controller is nothing however a controller that handles all requests for an internet site. They’re usually utilized in Internet functions to implement workflows.

DispatcherServlet of Spring MVC - 10 things Java Developer should know

3) Like every other Servlet, DispatcherServlet of Spring MVC framework can also be declared and configured in internet.xml file as proven under:

<web-app>

<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.internet.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>*</url-pattern>
</servlet-mapping>

</web-app>

4) The DispatcherServlet is an precise Servlet, it inherits from the HttpServlet base class. Servlet engine like Tomcat create an occasion of this class and calls it varied life-cycle strategies e.g. init(), service() and destroy().

5) The DispatcherServlet offers a Single level of entry to your Spring MVC internet software. As I stated earlier than, it handles all incoming requests.  See how Spring MVC works internally for extra particulars of the interior workings of Spring.

6) Spring’s DispatcherServlet can also be fully built-in with the Spring IoC container and as such means that you can use each function of the Spring framework like dependency injection.

7) The dispatcher servlet is configured as load-on-startup = 1 which implies this Servlet must be created by Servlet container while you deploy the appliance quite than creating when a request arrived for this request.

That is carried out to scale back the response time of the primary request as a result of DispatcherServlet does a number of job on the startup to scan and discover all controllers and request mappings. See the Java Internet Fundamentals course on Pluralsight by Kevin Jones to be taught extra about load-on-startup and different servlet fundamentals.

8) Throughout initialization of DispatcherServlet, the Spring MVC framework will search for a file named [servlet-name]-servlet.xml within the WEB-INF listing of your internet software and create the beans outlined there e.g. if servlet title is “SpringMVC” as proven within the above internet.xml configuration then it is going to search for a file named SpringMVC-Servlet.xml.

It additionally overrides the definitions of any beans outlined with the identical title within the international scope. You may change the precise location of this configuration file through the use of contextConfigLocation servlet initialization parameter.

DispatcherServlet of Spring MVC

9) Within the Spring MVC framework, every DispatcherServlet has its personal WebApplicationContext (see Spring in Motion), which inherits all of the beans already outlined within the root WebApplicationContext. These inherited beans may be overridden within the servlet-specific scope, and new scope-specific beans may be outlined regionally to a given servlet occasion.

10) The DispatcherServlet of Spring MVC framework may return the last-modification-date, as specified by the Servlet API. It determines the final modification date by searching for an applicable handler mapping and take a look at if the handler that’s discovered implements the LastModified interface. If sure, then it calls the getLastModified(request) methodology of the LastModified interface, and worth is returned to the shopper.

That is all concerning the DispatcherServlet of Spring MVC framework. As I stated, the DispatcherServlet is the spine of Spring MVC and serves as the primary controller which routes totally different HTTP requests to corresponding Spring Controllers. It’s an implementation of the Entrance Controller design sample and offers a single entry level to your Spring internet software.

You configure DispatcherServlet within the internet.xml however offers it load-on-startup worth 1 to counsel container to load this Servlet throughout startup and never when a request comes up. This reduces response time for the primary request.

Additional Studying
Spring Grasp Class – Newbie to Knowledgeable
Spring Framework 5: Newbie to Guru
REST with Spring by Baeldung

Different Spring associated articles chances are you’ll prefer to discover

  • Distinction between @RestControler and @Controller in Spring MVC? (reply)
  • 10 free programs to be taught Spring Framework (programs)
  • 23 Spring MVC Interview questions for two to three years skilled (listing)
  • 10 free programs to be taught Spring Boot for novices (programs)
  • What’s the usage of DispatcherServlet in Spring MVC? (reply)
  • 10 Programs to be taught Microservices with Spring in Java (programs)
  • The best way to allow Spring safety in Java software? (reply)
  • 5 Books and Programs to be taught RESTful Internet Companies in Java (programs)
  • Does Spring certification assist in Job and Profession? (article)
  • 20 Spring Boot Interview Questions for Java builders (questions)
  • The best way to Crack the Spring Skilled Certification (information
  • 10 Frameworks for  full-stack Java builders (frameworks)
  • High 5 Spring Certification Mock Exams (listing)
  • High 5 Programs to turn into full-stack JAva builders (programs)
  • Distinction between @Autowired and @Injection annotations in Spring? (reply)
  • 5 Spring and Hibernate on-line programs for Java builders (listing)

Thanks for studying this text to date. In case you like this text then please share it with your pals and colleagues. If in case you have any questions, strategies, or suggestions or you will have any level about dispatcher servlet which isn’t talked about right here however value figuring out then please drop a remark and I will embrace it in the primary article.

P. S. – In case you do not thoughts studying from free assets then you can even try my listing of  Free Spring Core and Spring Boot programs for Java builders to begin your journey. The listing accommodates free on-line programs to be taught Spring Core, Spring MVC, and Spring Boot. 



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments