What is the purpose of DispatcherServlet inwards Spring MVC? Interview Question

How DispatcherServlet plant internally inward Spring MVC or What is the role of DispatcherServlet inward Spring is roughly of the often asked Spring MVC Interview Question. You powerfulness convey seen them already during your Java spider web evolution interview. In this article, I'll response these interrogation yesteryear explaining What is DispatcherServlet too its importance inward Spring MVC. The DispatcherServlet is 1 of the of import components of Spring MVC spider web framework too acts every bit a Front Controller. Similar to other Java spider web framework, Spring MVC every bit good uses a front controller to have all incoming asking too delegates to other components for farther processing e.g. Spring MVC controllers which are annotated using @Controller notation too ViewResolvers e.g InternalResourceViewResolver class.

Influenza A virus subtype H5N1 Front Controller (see Patterns of Enterprise Application Architecture) is a mutual blueprint inward spider web application too used to have asking too delegate to other components inward the application for actual processing. The DispatcherServlet is a forepart controller e.g. it provides a unmarried entry signal for a client asking to Spring MVC spider web application too forwards asking to Spring MVC controllers for processing.


How does DispatcherServlet know that which asking should last forwarded to which Controller? Well, Spring uses mapping handlers for that, which I volition explicate yous inward adjacent department when we'll run into how DispatcherServlet plant internally. Apart from beingness a forepart controller, DispatcherServlet every bit good plays an of import role inward persuasion resolution, mistake handling, locale resolution, subject resolution etc.




How to configure DispatcherServlet inward Spring?

The DispatcherServlet is similar whatever other Servlet shape too it has to last declared within the deployment descriptor or web.xml file every bit shown below:

<servlet>     <servlet-name>dispatcher</servlet-name>     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>     <init-param>         <param-name>contextConfigLocation</param-name>         <!--Defaults to WEB-INF\dispatcher-servlet.xml -->         <param-value>classpath:mvc-config.xml</param-value>     </init-param> </servlet> 

Its URL blueprint is unremarkably "*" so that all incoming asking should become through Dispatcher servlet every bit shown below:

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

I haven't shown hither but, DispatcherServlet is every bit good unremarkably preloaded using the load-on-startup tag of the deployment descriptor. You tin give null or positive value on this tag to pre-load a servlet, otherwise, the servlet volition exclusively last loaded when a asking volition arise.

If your servlet does a lot of chore on initialization e.g. DispatcherServlet which initializes all the beans declared inward its spider web context e.g. controllers, persuasion resolvers, too mapping handlers too so it could deadening downward the response time.


Btw, it's non the exclusively agency to declare DispatcherServlet inward Spring MVC. From Spring 3.2 too Servlet 3.0 specification, yous tin programmatically declare DispatcherServlet using ServletContainerInitializer interface. This is a Servlet 3,0 characteristic which allows Servlet 3.0 compliant containers  e.g. Tomcat vii or higher to scan too charge whatever shape which implements this interface.

Spring provides an implementation of this interface every bit SpringServletContainerInitializer too a convenient shape called AbstractAnnotationConfigDispatcherServletInitialzer inward Spring 3.2 to configure DispatcherServlet without deployment descriptor.

This shape implements SpringServletContainerInitializer thus automatically picked yesteryear Servlet 3.0 compliant containers. See Spring inward Action fourth Edition to know to a greater extent than nearly configuring DispatcherServlet using Java Configuration.

 How DispatcherServlet plant internally inward Spring MVC or What is the role of DispatcherSer What is the work of DispatcherServlet inward Spring MVC? Interview Question



How dispatcher servlet plant internally?

As I said, DispatcherServlet wears many hats inward Spring. It acts every bit a forepart controller too provides a unmarried entry signal for the application. It too so uses handler mappings too handler adapters to map a asking to the Spring MVC controllers. It uses @Controller too @RequestMapping notation for that purpose.

Once the asking is processed yesteryear Spring MVC controller, it returns a logical persuasion lift instead of the view. Though, yous tin fifty-fifty configure Controler's handler methods to non homecoming whatever View lift yesteryear declaring homecoming type every bit void. You tin fifty-fifty work @ResponseBody notation inward the instance of REST to straight write the output to the HTTP response body. See REST alongside Spring course of education yesteryear Eugen to larn to a greater extent than nearly developing RESTful spider web services using Spring MVC.

When DispatherServlet receives persuasion name, it consults the ViewResolver to discovery the right view. There is a chain of ViewResolver is maintained at Spring MVC framework. They endeavour to resolve the logical persuasion lift into a Physical resources e.g. a JSP page or a FreeMaker or Velocity template.

The ViewResolver are invoked inward an order, if start inward the chain non able to resolve the persuasion too so it returns nix too adjacent ViewResolver inward the chain is consults. Once the right persuasion is found, DispatcherServlet forwards the asking along alongside Model information to the View for rendering e.g. a JSP page.


By default, DispatcherServlet uses InternalResourceViewResolver which uses prefix too suffix to convert a logical persuasion lift e.g. "home" to /WEB-INF/home.jsp. The View interface every bit good has getContentType() method, which returns content type the persuasion produces (JstlView has text/html). This is unremarkably the default content type for requests handled yesteryear the dispatcher servlet inward Spring.

Here is a dainty diagram which explains how DispatcherServlet plant internally inward Spring MVC

 How DispatcherServlet plant internally inward Spring MVC or What is the role of DispatcherSer What is the work of DispatcherServlet inward Spring MVC? Interview Question



In short, DispatcherServlet is used next things inward Spring MVC
- receives all asking every bit Front Controller  and provides a unmarried entry signal to the application
- mapping requests to right Spring MVC controller
- Consulting ViewResolvers to discovery right View
- forwarding asking to chosen View for rendering
- returning the response to the client
- creates web-context to initialize the spider web specific beans e.g. controllers, persuasion resolvers too handler mapping

That's all nearly what is the work of DispatcherServlet inward Spring framework. It's is 1 of the fundamental components of Spring MVC which is used to have all incoming asking too forrard them to right controllers for actual processing. It finds the right controllers yesteryear using handler mappings e.g. SimpleUrlHandlerMapping or BeanNameUrlHandlerMapping, which cheque if the edible bean lift is same every bit persuasion lift too the edible bean implements the View interface.

If yous are using annotations too so it tin every bit good work @Controller and @RequestMapping annotations to discovery the right controller to procedure a detail request. Once the asking is processed yesteryear controller it returns a logical persuasion lift to DispatcherServlet.

The DispatcherServlet too so consults ViewResolver too LocalResolvers to discovery the right View to homecoming the output. Once the right View is chosen, it forwards the asking to the View for rendering the response.

Further Learning
Spring Security Fundamentals
Spring Master Class - Beginner to Expert
Spring too Hibernate for Beginners
Spring MVC books too resources
Spring Web Application Developer Certification

Thanks for reading this article so far. If yous similar this interview interrogation too so delight portion alongside your friends too colleagues. If yous convey whatever interrogation or proposition too so delight drib a comment too I'll endeavour to discovery an response for you. 

P.S. - If yous desire to larn how to prepare RESTful Web Services using Spring Framework, cheque out Eugen Paraschiv's REST alongside Spring course. He has late launched the certification version of the course, which is amount of exercises too examples to farther cement the existent footing concepts yous volition larn from the course.

Subscribe to receive free email updates:

0 Response to "What is the purpose of DispatcherServlet inwards Spring MVC? Interview Question"

Posting Komentar