Saturday, May 31, 2014

web.xml and Servlet Version

web.xml is the web application deployment descriptor. It is used when a web application is deployed to a servlet container (such as Tomcat) at deployment time. The bare bone of this file looks like so (taken from my Spring MVC example): 

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app id="webApp_ID" version="2.5"
      xmlns:xsi="http://wwww.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">
       
        <display-name>Book Club</display-name>
       
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/xysample-servlet.xml
            </param-value>
        </context-param>

        <listener>
         <listener-class>
          org.springframework.web.context.ContextLoaderListener
         </listener-class>
        </listener>
       
        <servlet>
          <servlet-name>xysample</servlet-name>
            <servlet-class>
              org.springframework.web.servlet.DispatcherServlet
            </servlet-class>
          <load-on-startup>1</load-on-startup>
        </servlet>

        <servlet-mapping>
            <servlet-name>xysample</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    </web-app>


In a Eclipse Maven project, since we create web.xml manually, the item worth noting is the version number. It refers to the Servlet API version. This number should be corresponding to the Tomcat version, the Java environment that the app is running in. Following chart, taken from Tomcat site shows the compatibility:


Servlet Spec
JSP Spec
EL Spec
WebSocket Spec
Apache Tomcat version
Actual release revision
Support Java Versions
3.1
2.3
3.0
1.0
8.0.x
8.0.3 (beta)
7 and later
3.0
2.2
2.2
1.0
7.0.x
7.0.52
6 and later
(WebSocket 1.0 requires 7 or later)
2.5
2.1
2.1
N/A
6.0.x
6.0.39
5 and later
2.4
2.0
N/A
N/A
5.5.x (archived)
5.5.36 (archived)
1.4 and later
2.3
1.2
N/A
N/A
4.1.x (archived)
4.1.40 (archived)
1.3 and later
2.2
1.1
N/A
N/A
3.3.x (archived)
3.3.2 (archived)
1.1 and later

Also, by looking at the servlet-api.jar file under tomcat\lib, we could also verify that version 2.5 is indeed what we should use: 





N.B. Failure of specifying this number will not get you the features provided by the underline servlet api.

N.B. If I had elected to create a "Dynamic Web Application" from Eclipse, I would have selected the version from the ‘Dynamic web module version‘ dropdown list.



No comments:

Post a Comment