Time for action – configuring the dispatcher servlet

The dispatcher servlet is what examines the incoming request URL and invokes the right corresponding controller method. In our case, the welcome method from the HomeController class needs to be invoked if we enter the http://localhost:8080/webstore/ URL on the browser. So let's configure the dispatcher servlet for our project:

  1. Create web.xml under the src/main/webapp/WEB-INF/ directory in your project and enter the following content inside web.xml and save it:
    <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
      <servlet>
        <servlet-name>DefaultServlet</servlet-name>
        <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class>
      </servlet>
      
      <servlet-mapping>
        <servlet-name>DefaultServlet</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    
    </web-app>
  2. Now create one more xml file called DefaultServlet-servlet.xml under the same src/main/webapp/WEB-INF/ directory and enter the following content into it and save it:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    
    
      <mvc:annotation-driven />
      <context:component-scan base-package="com.packt.webstore" />
      
      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
      </bean>
    
    </beans>

What just happened?

If you know about servlet programming, you might be quite familiar with the servlet configuration and web.xml. In web.xml, we configured a servlet named DefaultServlet, which is more or less similar to any other normal servlet configuration. The only difference is that we have not created any servlet class for that configuration. Instead, the servlet class (org.springframework.web.servlet.DispatcherServlet) is provided by the Spring MVC framework, and we make use of it in web.xml. After this step, our configured DispatcherServlet (DefaultServlet) will be ready to handle any requests that come to our application on runtime and will dispatch the request to the correct controller's method.

However, DispatcherServlet should know where our controllers and view files are located in our project, and only then can it properly dispatch the request to the correct controllers. So we have to give some hint to DispatcherServlet to locate the controllers and view files. This is what we configured in step 2 through the DispatcherServlet-servlet.xml file.

Don't worry if you are not able to understand each and every configuration in web.xml and DispatcherServlet-servlet.xml; we will take a look at these configuration files in next chapter. As of now, just remember that this is a one-time configuration that is needed to run our project successfully.

Deploying our project

We successfully created the project in the last section, so you might be curious to know what would happen if we run our project now. As our project is a web project, we need a web server to run it.