To configure the front controller servlet, DispatcherServlet, in an XML-based configuration, we need to add the following XML code in the web.xml file:
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
In the preceding XML code, at first, we configured the DispatcherServlet. Then, we mentioned the context configuration location, /WEB-INF/spring-mvc-context.xml. We set the load-on-startup value as 1, so the servlet container will load this servlet upon startup. In the second part, we defined the servlet-mapping tag to map a URL / to DispatcherServlet. Now, we will define the Spring application context in the next step.
It is good to configure the load-on-startup element under the DispatcherServlet configuration to load it at the highest priority. This is because, in a cluster environment, you might face timeout issues if Spring is not up and you get a large number of calls hitting your web app once it's deployed.