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

Advertisement

Masukkan script iklan 970x90px

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

Jumat, 16 Februari 2001

One of the oft asked Spring MVC Interview questions is almost explaining the catamenia of spider web asking i.e. how an HTTP asking is processed from outset to end. In other words, explaining the flow of asking inwards Spring MVC. Since many of my readers inquire this inquiry fourth dimension as well as again, I idea to summarize the catamenia of asking processing inwards a brusque 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 expect into web.xml as well as discovery the Servlet or Filter which is mapped to that item 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 catamenia of asking inwards whatsoever Spring MVC based Java spider web application.

Remember, Web container e.g. Tomcat is responsible for creating Servlet as well as Filter instances as well as invoking their diverse life-cycle methods e.g. init(), service(), destroy(). In the instance of HTTP request, HttpServlet handles that as well as depending upon the HTTP asking method diverse doXXX() method is invoked past times container e.g. doGet() to procedure GET asking as well as doPost() to procedure POST request.

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

Though it is non mandatory, you lot tin convey other servlet mapped to other URL if you lot desire to, precisely if you lot 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, you lot tin run across 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 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 likewise purpose specific asking processing annotations e.g. @GetMapping or @PostMapping. Controller classes are likewise identified using @Controller as well as @RestController (in the instance of RESTful Web Services) annotations. See REST amongst Spring course of written report past times Eugen to acquire how to educate RESTful Web Service using Spring inwards depth.

For example, below bird is a Controller which volition procedure whatsoever 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 amongst @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 as well as 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 amongst model information as well as 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 overstep to whatsoever 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) as well as it's likewise a Single betoken of entry - grip all incoming requests, precisely in i lawsuit again that depends upon your URL blueprint mapping as well as 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 straight to /, precisely thus the exception for treatment static resources needs to live on configured. If you lot expect at the web.xml configuration it likewise pre-loaded using the load-on-startup tag.



Spring MVC move Flow

It's been often said that a motion-picture demo is worth a K words as well as this is real truthful inwards the instance of agreement organization architecture as well as workflow of your application. Whatever I convey said inwards in a higher house article, tin live on easily inferred past times looking at next diagram which explains workflow of Spring MVC framework:

RESTful Web Service asking is likewise non real dissimilar from this. It follows the same path precisely inwards the instance of REST, the Controller methods are annotated with @ResponseBody which way it doesn't homecoming a logical thought advert to DispatcherServlet, instead it write the output straight to HTTP response body. See Spring REST majority to acquire to a greater extent than almost how to educate RESTful Web Services using Spring.

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


In summary, hither is the catamenia 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 as well as @RequestMapping annotations.

3) Spring MVC Controller thus returns a logical thought advert as well as 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 as well as it renders the output depending on the model data

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

That's all almost what is the catamenia of Spring MVC or how an HTTP asking is processed past times Spring MVC. This is real basic precisely of import noesis almost Spring MVC framework as well as every Java as well as Spring developer should live on familiar amongst this. If you lot know how your HTTP asking is processed thus you lot tin non exclusively empathize the issues improve precisely likewise troubleshoot thus easily as well as quickly.

Further Reading
Spring Framework 5: Beginner to Guru
Spring Master Class - Beginner to Expert
Spring Certification
5 Best books to acquire Spring MVC
Spring Interview Questions

Thanks a lot for reading this article thus far. If you lot similar this article thus delight percentage amongst your friends as well as colleagues. If you lot convey whatsoever inquiry or proffer thus delight drib a Federal Reserve notation as well as I'll endeavour to respond your question.

P.S. - If you lot desire to acquire how to educate RESTful Web Service using Spring MVC inwards depth, I propose you lot bring together the REST amongst Spring certification class past times Eugen Paraschiv. One of the best course of written report to acquire REST amongst Spring MVC.