2

How Spring MVC Framework works? How HTTP Request is processed?

 1 week ago
source link: https://javarevisited.blogspot.com/2017/06/how-spring-mvc-framework-works-web-flow.html#axzz8Xqm2me00
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How Spring MVC Framework works? How HTTP Request is processed?

Hello guys, one of the frequently asked Spring MVC Interview questions is about explaining the flow of web requests i.e. how an HTTP request is processed from start to end. In other words, explaining the flow of requests in Spring MVC. Since many of my readers ask this question time and again, I thought to summarize the flow of request processing in a short article. It all starts with the client, which sends a request to a specific URL. When that request hits the web container like Tomcat it looks into web.xml and finds the Servlet or Filter which is mapped to that particular URL. It the delegate that Servlet or Filter to process the request. Since Spring MVC is built on top of Servlet, this is also the initial flow of requests in any Spring MVC based Java web application.

Loaded: 0.17%

Remember, Web container like Tomcat is responsible for creating Servlet and Filter instances and invoking their various life-cycle methods like init(), service(), destroy(). In the case of an HTTP request, HttpServlet handles that, and depending upon the HTTP request method various doXXX() method is invoked by container like doGet() to process GET request and doPost() to process POST request.

If you remember, to enable Spring MVC, we need to declare the DispatcherServlet from the Spring MVC jar into web.xml. This Servlet listens for a URL pattern * as shown in below web.xml, which means all request is mapped to DispatcherServlet.

Though it is not mandatory, you can have other servlet mapped to other URL if you want to, but if you are using Spring MVC to develop a web application or RESTful web service, it makes sense to pass through all requests via DispatcherServlet.

By the way, if you are new to the Spring framework then I also suggest you join a comprehensive and up-to-date course to learn Spring in depth. If you need recommendations, I highly suggest you take a look at Spring Framework 6: Beginner to Guru, one of the comprehensive and hands-on courses to learn modern Spring. It' also the most up-to-date and covers Spring 6.

How Spring MVC process an HTTP Request?

Here is the web.xml configuration for Spring MVC, you can see that Dispatcher Servlet is mapped to all request using URL pattern *

<web-app>

<!-- The front controller of this Spring Web application, responsible 
for handling 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 pattern is important, if the request matches the URL pattern of DispatcherServlet then it will be processed by Spring MVC otherwise not. The DispatcherServlet passes the request to a specific controller depending on the URL requested. 


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK