Thursday, October 31, 2019

How Dispatcherservlet Procedure A Asking Inward Saltation Mvc Application?

One of the mutual inquiry inwards Spring MVC is, how does DispatcherServlet procedure a asking inwards Spring MVC? or What is the component division of DispatcherServlet inwards Spring MVC framework? This is an first-class inquiry to whatever Java or Spring spider web developer, the answer to this inquiry shows how much yous know nearly Spring MVC framework as well as its working. Actually, DispatcherServlet plays a pregnant component division inwards Spring MVC. It acts equally a front end controller, as well as all incoming asking passes through it, of course, yous tin configure this inwards URL designing of DispatcherServlet proclamation inwards web.xml, but this is the instance for many Spring based spider web application.

The undertaking of DispatcherServlet is to notice the correct controller for processing the asking as well as thus when the handler method returns the logical thought name, it too consults thought resolvers to meet the actual view. Once the existent thought is found, as well as output is rendered, it sends the response dorsum to the client.

The DispatchServlet is an implementation of the front end controller designing (see Patterns of Enterprise Application Architecture), but it does to a greater extent than than that. It non alone handles all asking but it is too responsible for thought resolution, locale resolution as well as topic resolution inwards Spring MVC framework, but the most of import undertaking of the DispatcherServlet is to finding the mapping handlers as well as routing the asking to them. I'll explicate that inwards the side yesteryear side paragraph.

Btw, if yous convey worked inwards Spring MVC thus yous should already convey about basic ideas nearly things works, but if yous don't convey whatever prior sense as well as simply started amongst Spring framework as well as Spring MVC thus I propose yous to showtime acquire through a comprehensive course of didactics similar Spring Framework 5: Beginner to Guru course of didactics on Udemy.  One of the most useful courses on Spring framework




How does DispatcherServlet procedure a asking inwards Spring MVC?

As I said before, Dispatcher Servlet is used to grip all incoming requests as well as road them through dissimilar Spring Controllers for farther processing, but how does it notice which application needs to hold upwards routed to which controller or handlers?

To compass this, DispatcherServlet uses HandlerMapping implementations. It tin grip whatever implementation of HandlerMapping interface, similar pre-built or customized implementations provided yesteryear the application to road incoming requests to handler objects.

By default, it uses BeanNameUrlHandlerMapping as well as DefaultAnnotationHandlerMapping, which is driven using @RequestMapping annotation.

In gild to notice the correct methods for treatment the request, it scans through all the classes declared using @Controller annotation as well as it too uses @RequestMapping annotation to notice the types as well as methods responsible for treatment requests.

1. The @RequestMapping annotation tin map the asking yesteryear path, for illustration @RequestMapping("/home"),

2. By HTTP method like
@RequestMapping("/home", method=RequestMethod.GET)

3. By asking parameters like
@RequestMapping("/home", method=RequestMethod.POST, params="param1")

4. And, yesteryear presence of http asking header like
@RequestMapping("/home", header="content-type=text/*"). 


You tin too apply @RequestMapping annotation at the course of didactics degree to filter incoming requests.

Though, yous should recall that the @RequestMapping annotations volition alone hold upwards processed if a corresponding HandlerMapping (for type-level annotations) and/or HandlerAdapter (for method-level annotations) is introduce inwards the dispatcher servlet configuration file.

This is the instance yesteryear default. However, if yous are defining custom HandlerMappings or HandlerAdapters, thus yous demand to brand certain that a corresponding custom DefaultAnnotationHandlerMapping and/or AnnotationMethodHandlerAdapter is defined equally good - provided that yous intend to exercise @RequestMapping.

After processing the request, the Controller returns the logical thought elevate as well as model to DispatcherServlet. It thus consults to thought resolvers to notice the actual View to homecoming the output. The thought resolution strategy tin hold upwards specified using a ViewResolver implementation, yesteryear default, DispatcherServlet uses InternalResourceViewResolver to convert logical thought elevate to the actual View object similar a JSP.

After this DispatcherServlet contacts the chosen view, similar a JSP file amongst model information as well as it renders the output depending on the model data. This rendered output is returned to the customer equally a response.

Sometimes yous don't fifty-fifty demand a thought similar inwards the instance of RESTful Web services. Their handler method lead writes into response using @ResponseBody annotation as well as DispatcherServlet homecoming the answer to the client. See REST amongst Spring Certification class to acquire to a greater extent than nearly developing as well as testing RESTful Web service using Spring MVC.

Also, inwards the Spring MVC framework, a spider web application tin define whatever publish of DispatcherServlet. Each servlet volition operate inwards its ain namespace, loading its ain application context amongst mappings, handlers, etc. Only the root application context equally loaded yesteryear ContextLoaderListener, if any, volition hold upwards shared.

DispathcherServlet inwards Spring MVC

Influenza A virus subtype H5N1 flick is worth a one thousand words, fifty-fifty a clumsy i similar this which nicely summarise what happens when a asking hitting to boundary container as well as how DispatcherServlet procedure a asking inwards Spring MVC application:

 One of the mutual inquiry inwards Spring MVC is How DispatcherServlet procedure a asking inwards Spring MVC Application?


That's all nearly how DispatcherServlet procedure a spider web asking inwards Spring MVC. As I said, it scans through all @Controler classes as well as @RequestMapping to notice out all the handler methods as well as exercise a mapping of it. It thus routes an incoming asking to appropriate handlers, as well as when they homecoming a logical thought name, it uses ViewResolver implementations to resolve the elevate into a View object, which could hold upwards backed amongst whatever JSP or FreeMarker page.

The thought thus renders the response which is sent to the customer yesteryear DispatcherServlet. In the instance of RESTful Web Services as well as @RestController classes or handler methods annotated amongst @ResponseBody, the response is lead sent to the customer equally no thought resolution happens.

Further Reading
How Spring MVC works?
Spring MVC Interview Questions

Thanks for reading this article thus far. If yous similar this Spring MVC tutorial, thus delight portion amongst your friends as well as colleagues. If yous convey whatever questions or feedback, thus delight driblet a note.

No comments:

Post a Comment