How Saltation Mvc Framework Works? How Http Asking Is Processed?
One of the ofttimes asked Spring MVC Interview questions is close explaining the period of time of spider web asking i.e. how an HTTP asking is processed from offset to end. In other words, explaining the flow of asking inwards Spring MVC. Since many of my readers enquire this inquiry fourth dimension in addition to again, I idea to summarize the period of time of asking processing inwards a curt article. It all starts amongst the client, which sends a asking to a specific URL. When that asking hitting the spider web container e.g. Tomcat it await into web.xml in addition to detect the Servlet or Filter which is mapped to that detail URL. It the delegate that Servlet or Filter to procedure the request. Since Spring MVC is built on top of Servlet, this is likewise the initial period of time of asking inwards whatsoever Spring MVC based Java spider web application.
Remember, Web container e.g. Tomcat is responsible for creating Servlet in addition to Filter instances in addition to invoking their diverse life-cycle methods e.g. init(), service(), destroy(). In the instance of HTTP request, HttpServlet handles that in addition to depending upon the HTTP asking method diverse doXXX() method is invoked yesteryear container e.g. doGet() to procedure GET asking in addition to doPost() to procedure POST request.
If y'all remember, to enable Spring MVC, nosotros demand to declare the DispatcherServlet from Spring MVC scandalise into web.xml. This Servlet listens for a URL blueprint * equally shown inwards below web.xml, which way all asking is mapped to DispatcherServlet.
Though it is non mandatory, y'all tin bring other servlet mapped to other URL if y'all desire to, simply if y'all are using Spring MVC to prepare spider web application or RESTful spider web service, it brand feel to exceed through all asking via DispatcherServlet.
Here is the web.xml configuration for Spring MVC, y'all tin meet that DispatcherServlet is mapped to all asking using URL blueprint *
The URL blueprint is important, if the asking matches the URL blueprint of DispatcherServlet in addition to thence it volition endure processed yesteryear Spring MVC otherwise not. The DispatcherServlet the passes the asking to a specific controller depending on the URL requested. How does DispatcherServlet know which asking needs to endure passed to which controller?
Well, it uses the @RequestMapping annotation or Spring MVC configuration file to detect out mapping of asking URL to unlike controllers. It tin likewise role specific asking processing annotations e.g. @GetMapping or @PostMapping. Controller classes are likewise identified using @Controller in addition to @RestController (in the instance of RESTful Web Services) annotations. See REST amongst Spring course of education yesteryear Eugen to larn how to prepare RESTful Web Service using Spring inwards depth.
For example, below degree is a Controller which volition procedure whatsoever asking having URI "/appointments". It likewise has @GetMapping, which way that method volition endure invoked when a GET asking is received for this URL. The method annotated amongst @PostMapping volition endure invoked if the customer sends a POST request to the "/appointments" URI.
After processing the request, Controller returns a logical thought name in addition to model to DispatcherServlet and it consults thought resolvers until an actual View is determined to homecoming the output. DispatcherServlet in addition to thence contacts the chosen thought e.g. Freemarker or JSP amongst model information in addition to it renders the output depending on the model data.
This Rendered output is returned to the customer equally HTTP response. On it's way dorsum it tin exceed to whatsoever configured Filter equally good e.g. Spring Security filter chain or Filters configured to convert the response to JSON or XML.
The DispatcherServlet from Spring MVC framework is an implementation of Front Controller Pattern (see Patterns of Enterprise Application Architecture) in addition to it's likewise a Single bespeak of entry - handgrip all incoming requests, simply i time again that depends upon your URL blueprint mapping in addition to your application.
It delegates requests for farther processing to additional components e.g. Controllers, Views, View Resolvers, handler mappers, exception handlers etc. It tin likewise map lead to /, simply in addition to thence the exception for treatment static resources needs to endure configured. If y'all await at the web.xml configuration it likewise pre-loaded using the load-on-startup tag.
Remember, Web container e.g. Tomcat is responsible for creating Servlet in addition to Filter instances in addition to invoking their diverse life-cycle methods e.g. init(), service(), destroy(). In the instance of HTTP request, HttpServlet handles that in addition to depending upon the HTTP asking method diverse doXXX() method is invoked yesteryear container e.g. doGet() to procedure GET asking in addition to doPost() to procedure POST request.
If y'all remember, to enable Spring MVC, nosotros demand to declare the DispatcherServlet from Spring MVC scandalise into web.xml. This Servlet listens for a URL blueprint * equally shown inwards below web.xml, which way all asking is mapped to DispatcherServlet.
Though it is non mandatory, y'all tin bring other servlet mapped to other URL if y'all desire to, simply if y'all are using Spring MVC to prepare spider web application or RESTful spider web service, it brand feel to exceed through all asking via DispatcherServlet.
Here is the web.xml configuration for Spring MVC, y'all tin meet that DispatcherServlet is mapped to all asking using URL blueprint *
<web-app> <!-- The front end controller of this Spring Web application, responsible for treatment all application requests --> <servlet> <servlet-name>Spring MVC Dispatcher Servlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/web-application-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>example</servlet-name> <url-pattern>*</url-pattern> </servlet-mapping> </web-app>
The URL blueprint is important, if the asking matches the URL blueprint of DispatcherServlet in addition to thence it volition endure processed yesteryear Spring MVC otherwise not. The DispatcherServlet the passes the asking to a specific controller depending on the URL requested. How does DispatcherServlet know which asking needs to endure passed to which controller?
Well, it uses the @RequestMapping annotation or Spring MVC configuration file to detect out mapping of asking URL to unlike controllers. It tin likewise role specific asking processing annotations e.g. @GetMapping or @PostMapping. Controller classes are likewise identified using @Controller in addition to @RestController (in the instance of RESTful Web Services) annotations. See REST amongst Spring course of education yesteryear Eugen to larn how to prepare RESTful Web Service using Spring inwards depth.
For example, below degree is a Controller which volition procedure whatsoever asking having URI "/appointments". It likewise has @GetMapping, which way that method volition endure invoked when a GET asking is received for this URL. The method annotated amongst @PostMapping volition endure invoked if the customer sends a POST request to the "/appointments" URI.
@Controller @RequestMapping("/appointments") public class AppointmentsController { @GetMapping public Map get() { return appointmentBook.getAppointmentsForToday(); } @PostMapping public String add(@Valid AppointmentForm appointment, BindingResult result) { if (result.hasErrors()) { return "appointments/new"; } appointmentBook.addAppointment(appointment); return "redirect:/appointments"; } }
After processing the request, Controller returns a logical thought name in addition to model to DispatcherServlet and it consults thought resolvers until an actual View is determined to homecoming the output. DispatcherServlet in addition to thence contacts the chosen thought e.g. Freemarker or JSP amongst model information in addition to it renders the output depending on the model data.
This Rendered output is returned to the customer equally HTTP response. On it's way dorsum it tin exceed to whatsoever configured Filter equally good e.g. Spring Security filter chain or Filters configured to convert the response to JSON or XML.
The DispatcherServlet from Spring MVC framework is an implementation of Front Controller Pattern (see Patterns of Enterprise Application Architecture) in addition to it's likewise a Single bespeak of entry - handgrip all incoming requests, simply i time again that depends upon your URL blueprint mapping in addition to your application.
It delegates requests for farther processing to additional components e.g. Controllers, Views, View Resolvers, handler mappers, exception handlers etc. It tin likewise map lead to /, simply in addition to thence the exception for treatment static resources needs to endure configured. If y'all await at the web.xml configuration it likewise pre-loaded using the load-on-startup tag.
Spring MVC function Flow
It's been often said that a film is worth a grand words in addition to this is real truthful inwards the instance of agreement organisation architecture in addition to workflow of your application. Whatever I bring said inwards to a higher house article, tin endure easily inferred yesteryear looking at next diagram which explains workflow of Spring MVC framework: RESTful Web Service asking is likewise non real unlike from this. It follows the same path simply inwards the instance of REST, the Controller methods are annotated with @ResponseBody which way it doesn't homecoming a logical thought get upward to DispatcherServlet, instead it write the output lead to HTTP response body. See Spring REST majority to larn to a greater extent than close how to prepare RESTful Web Services using Spring.
In summary, hither is the period of time of an HTTP asking inwards Java application created using Spring MVC framework:
1) Client sends an HTTP asking to a specific URL
2) DispatcherServlet of Spring MVC receives the request
2) It passes the asking to a specific controller depending on the URL requested using @Controller in addition to @RequestMapping annotations.
3) Spring MVC Controller in addition to thence returns a logical thought get upward in addition to model to DispatcherServlet.
4) DispatcherServlet consults thought resolvers until actual View is determined to homecoming the output
5) DispatcherServlet contacts the chosen thought (e.g. Thymeleaf, Freemarker, JSP) amongst model information in addition to it renders the output depending on the model data
6) The rendered output is returned to the customer equally response
That's all close what is the period of time of Spring MVC or how an HTTP asking is processed yesteryear Spring MVC. This is real basic simply of import cognition close Spring MVC framework in addition to every Java in addition to Spring developer should endure familiar amongst this. If y'all know how your HTTP asking is processed in addition to thence y'all tin non solely empathise the issues meliorate simply likewise troubleshoot in addition to thence easily in addition to quickly.
Further Reading
Spring Framework 5: Beginner to Guru
Spring Master Class - Beginner to Expert
Spring Certification
5 Best books to larn Spring MVC
Spring Interview Questions
Thanks a lot for reading this article thence far. If y'all similar this article in addition to thence delight part amongst your friends in addition to colleagues. If y'all bring whatsoever inquiry or proffer in addition to thence delight drib a Federal Reserve notation in addition to I'll endeavour to respond your question.
P.S. - If y'all desire to larn how to prepare RESTful Web Service using Spring MVC inwards depth, I advise y'all bring together the REST amongst Spring certification class yesteryear Eugen Paraschiv. One of the best course of education to larn REST amongst Spring MVC.
In summary, hither is the period of time of an HTTP asking inwards Java application created using Spring MVC framework:
1) Client sends an HTTP asking to a specific URL
2) DispatcherServlet of Spring MVC receives the request
2) It passes the asking to a specific controller depending on the URL requested using @Controller in addition to @RequestMapping annotations.
3) Spring MVC Controller in addition to thence returns a logical thought get upward in addition to model to DispatcherServlet.
4) DispatcherServlet consults thought resolvers until actual View is determined to homecoming the output
5) DispatcherServlet contacts the chosen thought (e.g. Thymeleaf, Freemarker, JSP) amongst model information in addition to it renders the output depending on the model data
6) The rendered output is returned to the customer equally response
That's all close what is the period of time of Spring MVC or how an HTTP asking is processed yesteryear Spring MVC. This is real basic simply of import cognition close Spring MVC framework in addition to every Java in addition to Spring developer should endure familiar amongst this. If y'all know how your HTTP asking is processed in addition to thence y'all tin non solely empathise the issues meliorate simply likewise troubleshoot in addition to thence easily in addition to quickly.
Further Reading
Spring Framework 5: Beginner to Guru
Spring Master Class - Beginner to Expert
Spring Certification
5 Best books to larn Spring MVC
Spring Interview Questions
Thanks a lot for reading this article thence far. If y'all similar this article in addition to thence delight part amongst your friends in addition to colleagues. If y'all bring whatsoever inquiry or proffer in addition to thence delight drib a Federal Reserve notation in addition to I'll endeavour to respond your question.
P.S. - If y'all desire to larn how to prepare RESTful Web Service using Spring MVC inwards depth, I advise y'all bring together the REST amongst Spring certification class yesteryear Eugen Paraschiv. One of the best course of education to larn REST amongst Spring MVC.

0 Response to "How Saltation Mvc Framework Works? How Http Asking Is Processed?"
Post a Comment