What Is The Purpose Of Dispatcherservlet Inward Boundary Mvc? Interview Question
Hello guys, In today's article nosotros are going to hash out ane interesting as well as of import Spring MVC concepts, which is every bit good a pop Spring Interview question. How DispatcherServlet plant internally inwards Spring MVC or What is the role of DispatcherServlet inwards Spring MVC is around of the ofttimes asked Spring MVC Interview Questions. You mightiness convey seen them already during your previous Java spider web evolution interviews merely if you lot convey not, it's a expert enquiry to know. In this article, I'll reply these questions past times explaining What is DispatcherServlet and its importance inwards Spring MVC. The DispatcherServlet is ane of the of import components of the Spring MVC spider web framework as well as acts every bit a Front Controller.
Similar to other Java spider web framework similar Struts 1.x as well as Struts 2.x, Spring MVC every bit 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 every bit 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 designing inwards spider web application as well as used to have asking as well as delegate to other components inwards the application for actual processing. The DispatcherServlet is a front end controller similar it provides a unmarried entry betoken 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 hold upward forwarded to which Controller? Well, Spring uses mapping handlers for that, which I volition explicate to you lot inwards the side past times side department when we'll meet how DispatcherServlet plant internally.
Apart from existence a front end controller, DispatcherServlet every bit good plays an of import role inwards thought resolution, fault handling, locale resolution, subject resolution, etc. Btw, if you lot are non familiar alongside Spring inwards full general as well as Spring MVC inwards particular, you lot should every bit 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 every bit good covers Spring 5.0.
Its URL designing is commonly "*" so that all incoming asking should acquire through Dispatcher servlet every bit shown below:
I haven't shown hither but, DispatcherServlet is every bit good commonly preloaded using the load-on-startup tag of the deployment descriptor. You tin dismiss give null or positive value on this tag to pre-load a servlet, otherwise, the servlet volition alone hold upward loaded when a asking volition arise.
If your servlet does a lot of undertaking on initialization similar DispatcherServlet which initializes all the beans declared inwards its spider web context similar controllers, thought resolvers, as well as mapping handlers as well as then it could tiresome downward the response time.
Btw, it's non the alone agency to declare DispatcherServlet inwards Spring MVC. From Spring 3.2 as well as Servlet 3.0 specification, you lot tin dismiss 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 whatever course of report which implements this interface.
Spring provides an implementation of this interface every bit SpringServletContainerInitializer as well as a convenient course of report called AbstractAnnotationConfigDispatcherServletInitialzer inwards Spring 3.2 to configure DispatcherServlet without deployment descriptor.
This course of report 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 thought refer instead of the view. Though, you lot tin dismiss fifty-fifty configure Controler's handler methods to non homecoming whatever View refer past times declaring homecoming type every bit void.
You tin dismiss fifty-fifty utilisation @ResponseBody notation inwards the instance of REST to guide write the output to the HTTP response body. See REST alongside Spring course of report past times Eugen to larn to a greater extent than almost developing RESTful spider web services using Spring MVC.
When DispatherServlet receives thought name, it consults the ViewResolver to uncovering the right view. There is a chain of ViewResolver is maintained at the Spring MVC framework. They endeavour to resolve the logical thought refer into a Physical resources similar a JSP page or a FreeMaker or Velocity template.
The ViewResolver are invoked inwards order, if kickoff inwards the chain non able to resolve the thought as well as then it returns aught as well as side past times side ViewResolver inwards the chain is consults.
Once the right thought is found, DispatcherServlet forwards the asking along alongside 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 thought refer e.g. "home" to /WEB-INF/home.jsp. The View interface every bit good has getContentType() method, which returns content type the thought produces (JstlView has text/HTML). This is commonly the default content type for requests handled past times the dispatcher servlet inwards Spring. See Spring MVC for Beginners inwards 25 Steps to larn to a greater extent than almost asking handing inwards Spring MVC.
Here is a overnice diagram which explains how DispatcherServlet plant internally inwards Spring MVC
In short, DispatcherServlet is used next things inwards Spring MVC:
That's all almost what is the utilisation of DispatcherServlet inwards Spring framework. It's is ane of the primal components of Spring MVC which is used to have all incoming asking as well as frontward them to right controllers for actual processing. It finds the right controllers past times using handler mappings like SimpleUrlHandlerMapping or BeanNameUrlHandlerMapping, which banking concern stand upward for if the edible bean refer is the same every bit thought refer as well as the edible bean implements the View interface.
If you lot are using annotations as well as then it tin dismiss every bit good utilisation @Controller and @RequestMapping annotations to uncovering the right controller to procedure a item request. Once the asking is processed past times the controller it returns a logical thought refer to DispatcherServlet.
The DispatcherServlet as well as then consults ViewResolver as well as LocalResolvers to uncovering 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 hold upward forwarded to which Controller? Well, Spring uses mapping handlers for that, which I volition explicate to you lot inwards the side past times side department when we'll meet how DispatcherServlet plant internally.
Apart from existence a front end controller, DispatcherServlet every bit good plays an of import role inwards thought resolution, fault handling, locale resolution, subject resolution, etc. Btw, if you lot are non familiar alongside Spring inwards full general as well as Spring MVC inwards particular, you lot should every bit 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 every bit good covers Spring 5.0.
How to configure DispatcherServlet inwards Spring?
The DispatcherServlet is similar whatever other Servlet course of report as well as it has to hold upward 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 designing is commonly "*" so that all incoming asking should acquire 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 commonly preloaded using the load-on-startup tag of the deployment descriptor. You tin dismiss give null or positive value on this tag to pre-load a servlet, otherwise, the servlet volition alone hold upward loaded when a asking volition arise.
If your servlet does a lot of undertaking on initialization similar DispatcherServlet which initializes all the beans declared inwards its spider web context similar controllers, thought resolvers, as well as mapping handlers as well as then it could tiresome downward the response time.
Btw, it's non the alone agency to declare DispatcherServlet inwards Spring MVC. From Spring 3.2 as well as Servlet 3.0 specification, you lot tin dismiss 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 whatever course of report which implements this interface.
Spring provides an implementation of this interface every bit SpringServletContainerInitializer as well as a convenient course of report called AbstractAnnotationConfigDispatcherServletInitialzer inwards Spring 3.2 to configure DispatcherServlet without deployment descriptor.
This course of report 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 thought refer instead of the view. Though, you lot tin dismiss fifty-fifty configure Controler's handler methods to non homecoming whatever View refer past times declaring homecoming type every bit void.
You tin dismiss fifty-fifty utilisation @ResponseBody notation inwards the instance of REST to guide write the output to the HTTP response body. See REST alongside Spring course of report past times Eugen to larn to a greater extent than almost developing RESTful spider web services using Spring MVC.
When DispatherServlet receives thought name, it consults the ViewResolver to uncovering the right view. There is a chain of ViewResolver is maintained at the Spring MVC framework. They endeavour to resolve the logical thought refer into a Physical resources similar a JSP page or a FreeMaker or Velocity template.
The ViewResolver are invoked inwards order, if kickoff inwards the chain non able to resolve the thought as well as then it returns aught as well as side past times side ViewResolver inwards the chain is consults.
Once the right thought is found, DispatcherServlet forwards the asking along alongside 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 thought refer e.g. "home" to /WEB-INF/home.jsp. The View interface every bit good has getContentType() method, which returns content type the thought produces (JstlView has text/HTML). This is commonly the default content type for requests handled past times the dispatcher servlet inwards Spring. See Spring MVC for Beginners inwards 25 Steps to larn to a greater extent than almost asking handing inwards Spring MVC.
Here is a overnice diagram which explains how DispatcherServlet plant internally inwards Spring MVC
In short, DispatcherServlet is used next things inwards Spring MVC:
- Receives all asking every bit Front Controller and provides a unmarried entry betoken to the application
- Mapping requests to right Spring MVC controller
- Consulting ViewResolvers to uncovering 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, thought resolvers as well as handler mapping
That's all almost what is the utilisation of DispatcherServlet inwards Spring framework. It's is ane of the primal components of Spring MVC which is used to have all incoming asking as well as frontward them to right controllers for actual processing. It finds the right controllers past times using handler mappings like SimpleUrlHandlerMapping or BeanNameUrlHandlerMapping, which banking concern stand upward for if the edible bean refer is the same every bit thought refer as well as the edible bean implements the View interface.
If you lot are using annotations as well as then it tin dismiss every bit good utilisation @Controller and @RequestMapping annotations to uncovering the right controller to procedure a item request. Once the asking is processed past times the controller it returns a logical thought refer to DispatcherServlet.
The DispatcherServlet as well as then consults ViewResolver as well as LocalResolvers to uncovering 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 inwards 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 then delight portion alongside your friends as well as colleagues. If you lot convey whatever enquiry or proposition as well as then delight drib a comment as well as I'll endeavour to uncovering an reply for you. Spring Framework 5: Beginner to Guru
Spring Master Class - Beginner to Expert
Spring MVC for Beginners inwards 25 Steps
Other Spring related articles you lot may similar to explore this blog
- How Spring MVC plant internally? (answer)
- Difference betwixt @RestController as well as @Controller? (answer)
- 15 Spring Boot Interview Questions for Java Developers (questions)
- 3 ways to larn Spring Framework improve (article)
- Free Courses to larn Spring MVC as well as Spring Boot (courses)
- How to enable Spring safety inwards Java application? (answer)
- Does Spring certification help inwards Job as well as Career? (article)
- How to prepare for Spring Certification? (guide)
- 3 Best Practices Java Developers Can larn from Spring (article)
- Difference between @Autowired as well as @Injection annotations inwards 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 larn Microservices alongside Spring Boot as well as Spring Cloud (courses)
P.S. - If you lot desire to larn how to prepare RESTful Web Services using Spring Framework, banking concern stand upward for out Eugen Paraschiv's REST alongside Spring course. He has lately launched the certification version of the course, which is sum of exercises as well as examples to farther cement the existent globe concepts you lot volition larn from the course.
0 Response to "What Is The Purpose Of Dispatcherservlet Inward Boundary Mvc? Interview Question"
Post a Comment