Hello World Web App using Spring Annotations Part 2

In part 1 of this series, we set up a new web application project in Eclipse, using Maven and Sysdeo.

In this second of the Hello World web application posts, we’ll configure our application’s web.xml, and walk through the web.xml file line by line.

Modifying the web.xml file

The web.xml file is a Java web application standard.  Tomcat (and most other servers) look in your applications WEB-INF/web.xml file to learn important startup information about your application.

In our application, we configured /src/main/webapp to be the base of our web application, so the file we want to deal with is /src/main/webapp/WEB-INF/web.xml.  Lets take a look at the final version for HelloWebApp, then go over it line by line.

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
    <display-name>Hello Web App</display-name>
    
    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>hellowebapp.root</param-value>
    </context-param>
    
    <listener><listener-class>org.springframework.web.util.Log4jConfigListener</listener-class></listener>
    
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>    
    
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:/spring/dispatch-servlet.xml
        </param-value>
    </context-param>
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/log4j.properties</param-value>
    </context-param>


    <servlet>
        <servlet-name>dispatch</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                classpath:/spring/dispatch-servlet.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>dispatch</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>        

    <servlet-mapping>
        <servlet-name>dispatch</servlet-name>
        <url-pattern>/index.html</url-pattern>
    </servlet-mapping>
    
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>        
    
</web-app>

 

/src/main/webapp/WEB-INF/web.xml

Line 1: This is the standard J2EE web-app 2.4 declaration.

Line 6: This is the web-app name as it appears in the Tomcat manager.  Any string is fine here, as long as it is unique from other web applications.

Lines 8-11: Here we are defining a context parameter called "webAppRootKey".  Some libraries (notably log4j) use this key to keep web applications separated in their own namespace.  Since we are, in fact, using log4j, we added this parameter here.  Again, any string is fine as long as it is unique.

Line 13: This Listener declaration tells Spring to listen for a Log4j setup and to get details about it when it starts.  While not strictly required, it will prevent warning if you are using both Spring and Log4j (which we are).

Line 15-19: Spring’s ContextLoaderListener is use to bootstrap the web application’s context into being.  It also needs a context configuration file (XML in other words) which we define the location of in lines 21-26.

Line 21-26:  Here we define the location of Spring’s web context configuration.  Note that we are using a "classpath:" declaration instead of giving a full path name.  This makes it a bit easier to move the WAR file around.  Our Spring configuration is configured as /spring/dispatch-servlet.xml.  In the project space, this file will be at /src/main/resources/spring/dispatch-servlet (more on this file later).  Note to Spring old-timers: yes, this really should be an application-context.xml, but we don’t have one for this project.

Line 27-30: Just like it sounds: this is the master log4j.properties file.  In our project space, we’ll put this file at /src/main/webapp/WEB-INF/log4j.properties.

Line 33-43: This block defines the Spring dispatcher servlet, which will intercept requests into the web application and route them to the correct place.  You will notice again that we have identified a configuration file for the servlet (which is the same as the context for this project.. more on this later).

Line 45-53: These are standard J2EE servlet mappings.  Basically they say that we want any request for a *.html file to go through the dispatcher servlet.  I’ve also specifically included /index.html, which allows Spring to catch the "/" path request too.

Line 55-57: Standard J2EE welcome file list. If you request "HelloWebApp/", it will forward you to index.html (which, in turn will be caught by the servlet mapping directly above, and sent to dispatcher servlet).

Line 59: End of the web-app definition.

That’s it for part 2.  In part 3, we’ll add some Maven dependencies (including Spring), and review our Spring context configuration.

Leave a Reply

Your email address will not be published. Required fields are marked *