How Jump Mvc Framework Works? How Http Asking Is Processed?

One of the ofttimes asked Spring MVC Interview questions is close explaining the menstruation of spider web asking i.e. how an HTTP asking is processed from get-go to end. In other words, explaining the flow of asking inward Spring MVC. Since many of my readers enquire this interrogation fourth dimension in addition to again, I idea to summarize the menstruation of asking processing inward a curt article. It all starts alongside the client, which sends a asking to a specific URL. When that asking striking the spider web container e.g. Tomcat it await into web.xml in addition to discovery 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 menstruation of asking inward whatever 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 illustration of HTTP request, HttpServlet handles that in addition to depending upon the HTTP asking method diverse doXXX() method is invoked past times container e.g. doGet() to procedure GET asking in addition to doPost() to procedure POST request.

If yous remember, to enable Spring MVC, nosotros demand to declare the DispatcherServlet from Spring MVC jounce into web.xml. This Servlet listens for a URL designing * every bit shown inward below web.xml, which way all asking is mapped to DispatcherServlet.

Though it is non mandatory, yous tin give notice accept other servlet mapped to other URL if yous desire to, merely if yous are using Spring MVC to educate spider web application or RESTful spider web service, it brand feel to overstep through all asking via DispatcherServlet.


Here is the web.xml configuration for Spring MVC, yous tin give notice encounter that DispatcherServlet is mapped to all asking using URL designing *
<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 designing is important, if the asking matches the URL designing of DispatcherServlet thus it volition live on processed past times 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 live on passed to which controller?


Well, it uses the @RequestMapping annotation or Spring MVC configuration file to discovery out mapping of asking URL to dissimilar controllers. It tin give notice likewise piece of job specific asking processing annotations e.g. @GetMapping or @PostMapping. Controller classes are likewise identified using @Controller in addition to @RestController (in the illustration of RESTful Web Services) annotations. See REST alongside Spring course of teaching past times Eugen to larn how to educate RESTful Web Service using Spring inward depth.

For example, below shape is a Controller which volition procedure whatever asking having URI "/appointments". It likewise has @GetMapping, which way that method volition live on invoked when a GET asking is received for this URL. The method annotated alongside @PostMapping volition live on 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 thus contacts the chosen thought e.g. Freemarker or JSP alongside model information in addition to it renders the output depending on the model data.

This Rendered output is returned to the customer every bit HTTP response. On it's way dorsum it tin give notice overstep to whatever configured Filter every bit 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 betoken of entry - grip all incoming requests, merely 1 time to a greater extent than that depends upon your URL designing 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 give notice likewise map straight to /, merely thus the exception for treatment static resources needs to live on configured. If yous await at the web.xml configuration it likewise pre-loaded using the load-on-startup tag.



Spring MVC piece of job Flow

It's been often said that a motion-picture demo is worth a yard words in addition to this is really truthful inward the illustration of agreement organisation architecture in addition to workflow of your application. Whatever I accept said inward to a higher house article, tin give notice live on easily inferred past times looking at next diagram which explains workflow of Spring MVC framework:

RESTful Web Service asking is likewise non really dissimilar from this. It follows the same path merely inward the illustration of REST, the Controller methods are annotated with @ResponseBody which way it doesn't homecoming a logical thought cite to DispatcherServlet, instead it write the output straight to HTTP response body. See Spring REST mass to larn to a greater extent than close how to educate RESTful Web Services using Spring.

 is close explaining the menstruation of spider web asking i How Spring MVC Framework works? How HTTP Request is processed?


In summary, hither is the menstruation of an HTTP asking inward 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 thus returns a logical thought cite 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) alongside model information in addition to it renders the output depending on the model data

6) The rendered output is returned to the customer every bit response

That's all close what is the menstruation of Spring MVC or how an HTTP asking is processed past times Spring MVC. This is really basic merely of import cognition close Spring MVC framework in addition to every Java in addition to Spring developer should live on familiar alongside this. If yous know how your HTTP asking is processed thus yous tin give notice non exclusively empathise the issues amend merely likewise troubleshoot thus 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 thus far. If yous similar this article thus delight part alongside your friends in addition to colleagues. If yous accept whatever interrogation or proposition thus delight driblet a regime annotation in addition to I'll endeavour to reply your question.

P.S. - If yous desire to larn how to educate RESTful Web Service using Spring MVC inward depth, I advise yous bring together the REST alongside Spring certification class past times Eugen Paraschiv. One of the best course of teaching to larn REST alongside Spring MVC. 

1 Response to "How Jump Mvc Framework Works? How Http Asking Is Processed?"

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel