In Spring Web MVC, DispatcherServlet class works as the front controller. It is responsible to manage the flow of the spring mvc application.
The @Controller annotation is used to mark the class as the controller in Spring 3.
The @RequestMapping annotation is used to map the request url. It is applied on the method.

Spring MVC Execution Flow
Step 1: First request will be received by DispatcherServlet.
Step 2: DispatcherServlet will take the help of HandlerMapping and get to know the Controller class name associated with the given request.
Step 3: So request transfer to the Controller, and then controller will process the request by executing appropriate methods and returns ModelAndView object (contains Model data and View name) back to the DispatcherServlet.
Step 4: Now DispatcherServlet send the model object to the ViewResolver to get the actual view page.
Step 5: Finally DispatcherServlet will pass the Model object to the View page to display the result.
Now Let us understand
First of all, we create Dynamic web project in eclipse and following are the required files for the same:
- JavaController.java
- welcomePage.jsp
- web.xml
- welcome-servlet.xml
- index.jsp
JavaController.java
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.codeNuclear; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class JavaController { @RequestMapping("/codeNuclear") public ModelAndView helloWorld(ModelMap model, HttpServletRequest request) { String message = "Welcome to codeNuclear.com Spring MVC Sessions"; message += "<br>You Did it....!"; System.out.println(message); model.addAttribute("message", message); return new ModelAndView("welcomePage", "welcomeMessage", message); }//ModelAndView closed } |
welcomePage.jsp
0 1 2 3 4 5 6 7 8 9 10 11 |
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <body> <font face="verdana" size="2"> ${welcomeMessage} </font> </body> </html> |
web.xml
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>SpringMvcExample</display-name> <servlet> <servlet-name>welcome</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>welcome</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>/</welcome-file> </welcome-file-list> </web-app> |
welcome-servlet.xml
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.codeNuclear" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans> |
index.jsp
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>codeNuclear.com Spring MVC 3.x</title> </head> <body> <font size="2px" face="verdana"> Welcome... <a href="codeNuclear.html"><br> Click here to check the output.</a> </font> </body> </html> |
Practical Execution Flow
Step 1)Run the application, then
Step 2)Once you click on that link, container will check the URL pattern at
Step 3)DispatcherServlet then passes that request to our controller class.
Step 4)DispatcherServlet verifies this ‘codeNuclear’ name with the string in @RequestMapping(“-“) in our controller class if same it will executes the ModelAndView method, which gives ModelAndView object as return type.
0 1 2 |
return new ModelAndView("welcomePage", "welcomeMessage", message); |
Means first argument is ‘View’ page name [ Where we are sending our result ], second, third arguments are key,values.
Step 5)So DispatcherServlet search for the name welcomePage in /jsp folder with extension .jsp , once the file was opened you can access the data by using the key welcomeMessage [2nd parameter in ModelAndView object].
Check welcomePage.jsp and printing the result by calling the key ${welcomeMessage} from
Output


That’s it :-) Keep Learning…!!!
Enjoyed reading through this, very good stuff, thankyou .
I am just starting to learn about all of this. Thanks!
thanks a lot.. helped me very much specially “practicla execution flow”. but dint get where exactly welcome-servlet.xml gets invoked.
Welcome-Servlet.xml is dispatcher servlet i.e. front controller
very helpful
Step 3 : ModeAndView -> ModelAndView you are missing L.
and by the way I want to thank you.
Thank you for explaining with example. Very helpful.
Hi Ram,
We are glad to hear that.
Keep Learning and Sharing.
Nice Explanation. Thanks you.
I read this post fully regarding the comparison of most recent and previous technologies, it’s remarkable article.|