Hello guys, In today's article nosotros are going to utter over ane interesting as well as of import Spring MVC concepts, which is equally good a pop Spring Interview question. How DispatcherServlet industrial plant internally inward Spring MVC or What is the role of DispatcherServlet inward Spring MVC is around of the often asked Spring MVC Interview Questions. You powerfulness accept seen them already during your previous Java spider web evolution interviews exactly if you lot accept not, it's a practiced enquiry to know. In this article, I'll reply these questions past times explaining What is DispatcherServlet and its importance inward Spring MVC. The DispatcherServlet is ane of the of import components of the Spring MVC spider web framework as well as acts equally a Front Controller.
Similar to other Java spider web framework similar Struts 1.x as well as Struts 2.x, Spring MVC equally good uses a Front Controller (see Patterns of Enterprise Application Architecture) to have all incoming asking as well as delegates to other components for farther processing similar Spring MVC controllers which are annotated using @Controller notation as well as ViewResolvers similar the InternalResourceViewResolver class.
Similar to other Java spider web framework similar Struts 1.x as well as Struts 2.x, Spring MVC equally good uses a Front Controller (see Patterns of Enterprise Application Architecture) to have all incoming asking as well as delegates to other components for farther processing similar Spring MVC controllers which are annotated using @Controller notation as well as ViewResolvers similar the InternalResourceViewResolver class.
Influenza A virus subtype H5N1 Front Controller is a mutual blueprint inward spider web application as well as used to have asking as well as delegate to other components inward the application for actual processing. The DispatcherServlet is a front end controller similar it provides a unmarried entry signal for a client asking to Spring MVC spider web application as well as 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 to you lot inward the adjacent department when we'll meet how DispatcherServlet industrial plant internally.
Apart from beingness a front end controller, DispatcherServlet equally good plays an of import role inward sentiment resolution, fault handling, locale resolution, topic resolution, etc. Btw, if you lot are non familiar amongst Spring inward full general as well as Spring MVC inward particular, you lot should equally good join Spring Framework 5: Beginner to Guru course on Udemy, ane of the most up-to-date courses on Spring framework as well as it equally good covers Spring 5.0.
Its URL blueprint is normally "*" so that all incoming asking should acquire through Dispatcher servlet equally shown below:
I haven't shown hither but, DispatcherServlet is equally good normally 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 alone last loaded when a asking volition arise.
If your servlet does a lot of chore on initialization similar DispatcherServlet which initializes all the beans declared inward its spider web context similar controllers, sentiment resolvers, as well as mapping handlers as well as so it could ho-hum downwardly the response time.
Btw, it's non the alone means to declare DispatcherServlet inward Spring MVC. From Spring 3.2 as well as Servlet 3.0 specification, you lot tin programmatically declare DispatcherServlet using ServletContainerInitializer interface.
This is a Servlet 3,0 characteristic which allows Servlet 3.0 compliant containers similar Tomcat vii or higher to scan as well as charge whatsoever shape which implements this interface.
Spring provides an implementation of this interface equally SpringServletContainerInitializer as well as a convenient shape called AbstractAnnotationConfigDispatcherServletInitialzer inward Spring 3.2 to configure DispatcherServlet without deployment descriptor.
This shape implements SpringServletContainerInitializer therefore automatically picked past times Servlet 3.0 compliant containers. See Spring MVC controllers. It uses @Controller as well as @RequestMapping notation for that purpose.
Once the asking is processed past times the Spring MVC controller, it returns a logical sentiment mention instead of the view. Though, you lot tin fifty-fifty configure Controler's handler methods to non homecoming whatsoever View mention past times declaring homecoming type equally void.
You tin fifty-fifty utilisation @ResponseBody notation inward the instance of REST to straight write the output to the HTTP response body. See REST amongst Spring course of report past times Eugen to acquire to a greater extent than near developing RESTful spider web services using Spring MVC.
When DispatherServlet receives sentiment name, it consults the ViewResolver to notice the right view. There is a chain of ViewResolver is maintained at the Spring MVC framework. They elbow grease to resolve the logical sentiment mention into a Physical resources similar a JSP page or a FreeMaker or Velocity template.
The ViewResolver are invoked inward order, if get-go inward the chain non able to resolve the sentiment as well as so it returns naught as well as adjacent ViewResolver inward the chain is consults.
Once the right sentiment is found, DispatcherServlet forwards the asking along amongst Model information to the View for rendering similar a JSP page.
By default, DispatcherServlet uses InternalResourceViewResolver which uses prefix as well as suffix to convert a logical sentiment mention e.g. "home" to /WEB-INF/home.jsp. The View interface equally good has getContentType() method, which returns content type the sentiment produces (JstlView has text/HTML). This is normally the default content type for requests handled past times the dispatcher servlet inward Spring. See Spring MVC for Beginners inward 25 Steps to acquire to a greater extent than near asking handing inward Spring MVC.
Here is a squeamish diagram which explains how DispatcherServlet industrial plant internally inward Spring MVC
In short, DispatcherServlet is used next things inward Spring MVC:
That's all near what is the utilisation of DispatcherServlet inward Spring framework. It's is ane of the cardinal components of Spring MVC which is used to have all incoming asking as well as forrad them to right controllers for actual processing. It finds the right controllers past times using handler mappings like SimpleUrlHandlerMapping or BeanNameUrlHandlerMapping, which banking concern jibe if the edible bean mention is the same equally sentiment mention as well as the edible bean implements the View interface.
If you lot are using annotations as well as so it tin equally good utilisation @Controller and @RequestMapping annotations to notice the right controller to procedure a item request. Once the asking is processed past times the controller it returns a logical sentiment mention to DispatcherServlet.
The DispatcherServlet as well as so consults ViewResolver as well as LocalResolvers to notice the right View to homecoming the output. Once the right View is chosen, it forwards the asking to the View for rendering the response.
How does DispatcherServlet know that which asking should last forwarded to which Controller? Well, Spring uses mapping handlers for that, which I volition explicate to you lot inward the adjacent department when we'll meet how DispatcherServlet industrial plant internally.
Apart from beingness a front end controller, DispatcherServlet equally good plays an of import role inward sentiment resolution, fault handling, locale resolution, topic resolution, etc. Btw, if you lot are non familiar amongst Spring inward full general as well as Spring MVC inward particular, you lot should equally good join Spring Framework 5: Beginner to Guru course on Udemy, ane of the most up-to-date courses on Spring framework as well as it equally good covers Spring 5.0.
How to configure DispatcherServlet inward Spring?
The DispatcherServlet is similar whatsoever other Servlet shape as well as it has to last declared within the deployment descriptor or web.xml file equally 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 normally "*" so that all incoming asking should acquire through Dispatcher servlet equally shown below:
<servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
I haven't shown hither but, DispatcherServlet is equally good normally 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 alone last loaded when a asking volition arise.
If your servlet does a lot of chore on initialization similar DispatcherServlet which initializes all the beans declared inward its spider web context similar controllers, sentiment resolvers, as well as mapping handlers as well as so it could ho-hum downwardly the response time.
Btw, it's non the alone means to declare DispatcherServlet inward Spring MVC. From Spring 3.2 as well as Servlet 3.0 specification, you lot tin programmatically declare DispatcherServlet using ServletContainerInitializer interface.
This is a Servlet 3,0 characteristic which allows Servlet 3.0 compliant containers similar Tomcat vii or higher to scan as well as charge whatsoever shape which implements this interface.
Spring provides an implementation of this interface equally SpringServletContainerInitializer as well as a convenient shape called AbstractAnnotationConfigDispatcherServletInitialzer inward Spring 3.2 to configure DispatcherServlet without deployment descriptor.
This shape implements SpringServletContainerInitializer therefore automatically picked past times Servlet 3.0 compliant containers. See Spring MVC controllers. It uses @Controller as well as @RequestMapping notation for that purpose.
Once the asking is processed past times the Spring MVC controller, it returns a logical sentiment mention instead of the view. Though, you lot tin fifty-fifty configure Controler's handler methods to non homecoming whatsoever View mention past times declaring homecoming type equally void.
You tin fifty-fifty utilisation @ResponseBody notation inward the instance of REST to straight write the output to the HTTP response body. See REST amongst Spring course of report past times Eugen to acquire to a greater extent than near developing RESTful spider web services using Spring MVC.
When DispatherServlet receives sentiment name, it consults the ViewResolver to notice the right view. There is a chain of ViewResolver is maintained at the Spring MVC framework. They elbow grease to resolve the logical sentiment mention into a Physical resources similar a JSP page or a FreeMaker or Velocity template.
The ViewResolver are invoked inward order, if get-go inward the chain non able to resolve the sentiment as well as so it returns naught as well as adjacent ViewResolver inward the chain is consults.
Once the right sentiment is found, DispatcherServlet forwards the asking along amongst Model information to the View for rendering similar a JSP page.
By default, DispatcherServlet uses InternalResourceViewResolver which uses prefix as well as suffix to convert a logical sentiment mention e.g. "home" to /WEB-INF/home.jsp. The View interface equally good has getContentType() method, which returns content type the sentiment produces (JstlView has text/HTML). This is normally the default content type for requests handled past times the dispatcher servlet inward Spring. See Spring MVC for Beginners inward 25 Steps to acquire to a greater extent than near asking handing inward Spring MVC.
Here is a squeamish diagram which explains how DispatcherServlet industrial plant internally inward Spring MVC
In short, DispatcherServlet is used next things inward Spring MVC:
- Receives all asking equally Front Controller and provides a unmarried entry signal to the application
- Mapping requests to right Spring MVC controller
- Consulting ViewResolvers to notice right View
- Forwarding asking to chosen View for rendering
- Returning the response to the client
- Creates web-context to initialize the web-specific beans like controllers, sentiment resolvers as well as handler mapping
That's all near what is the utilisation of DispatcherServlet inward Spring framework. It's is ane of the cardinal components of Spring MVC which is used to have all incoming asking as well as forrad them to right controllers for actual processing. It finds the right controllers past times using handler mappings like SimpleUrlHandlerMapping or BeanNameUrlHandlerMapping, which banking concern jibe if the edible bean mention is the same equally sentiment mention as well as the edible bean implements the View interface.
If you lot are using annotations as well as so it tin equally good utilisation @Controller and @RequestMapping annotations to notice the right controller to procedure a item request. Once the asking is processed past times the controller it returns a logical sentiment mention to DispatcherServlet.
The DispatcherServlet as well as so consults ViewResolver as well as LocalResolvers to notice 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 Reading
Spring Framework 5: Beginner to Guru
Spring Master Class - Beginner to Expert
Spring MVC for Beginners inward 25 Steps
Other Spring related articles you lot may similar to explore this blog
Thanks for reading this article so far. If you lot similar this interview enquiry as well as so delight portion amongst your friends as well as colleagues. If you lot accept whatsoever enquiry or proposition as well as so delight drib a comment as well as I'll elbow grease to notice an reply for you. Spring Framework 5: Beginner to Guru
Spring Master Class - Beginner to Expert
Spring MVC for Beginners inward 25 Steps
Other Spring related articles you lot may similar to explore this blog
- How Spring MVC industrial plant internally? (answer)
- Difference betwixt @RestController as well as @Controller? (answer)
- 15 Spring Boot Interview Questions for Java Developers (questions)
- 3 ways to acquire Spring Framework amend (article)
- Free Courses to acquire Spring MVC as well as Spring Boot (courses)
- How to enable Spring safety inward Java application? (answer)
- Does Spring certification assistance inward Job as well as Career? (article)
- How to prepare for Spring Certification? (guide)
- 3 Best Practices Java Developers Can acquire from Spring (article)
- Difference between @Autowired as well as @Injection annotations inward Spring? (answer)
- 5 Spring as well as Hibernate online courses for Java developers (list)
- 5 Spring Boot courses for Java developers (courses)
- 5 courses to acquire Microservices amongst Spring Boot as well as Spring Cloud (courses)
P.S. - If you lot desire to acquire how to railroad train RESTful Web Services using Spring Framework, banking concern jibe out Eugen Paraschiv's REST amongst Spring course. He has late launched the certification version of the course, which is amount of exercises as well as examples to farther cement the existent globe concepts you lot volition acquire from the course.
No comments:
Post a Comment