web.xml example 

Joined:
04/09/2007
Posts:
753

February 10, 2010 23:39:37    Last update: February 10, 2010 23:39:37
Example web.xml that includes most frequently used elements. This sample is for Servlet Specification 2.4.
<?xml version="1.0" encoding="UTF-8"?> 
<web-app 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" 
         version="2.4"> 

        <!-- Appliocation name and description --> 
        <display-name>Example App</display-name> 
        <description>A web.xml example</description> 

        <!-- Set session timeout to 30 minutes --> 
        <session-config> 
            <session-timeout>30</session-timeout> 
        </session-config> 

        <!-- Context parameters -->
        <context-param> 
            <description>Enable debugging for the application</description> 
            <param-name>debug</param-name> 
            <param-value>true</param-value> 
        </context-param> 

        <context-param> 
            <description>Web master email</description> 
            <param-name>webmaster</param-name> 
            <param-value>webmaster@example.com</param-value> 
        </context-param> 

        <!-- Servlets -->
        <servlet>
            <servlet-name>action</servlet-name>
            <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
            <init-param>
                <param-name>config</param-name>
                <param-value>/WEB-INF/struts-config.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>

        <!-- Servlet mappings -->
        <servlet-mapping>
            <servlet-name>action</servlet-name>
            <url-pattern>*.do</url-pattern>
        </servlet-mapping>

        <!-- Filters -->
        <filter>
            <filter-name>loggingFilter</filter-name>
            <filter-class>com.example.LoggingFilter</filter-class>
            <init-param>
                <param-name>appender</param-name>
                <param-value>com.example.FileAppender</param-value>
            </init-param>
            <init-param>
                <param-name>logging-file</param-name>
                <param-value>/var/logs/myapp.log</param-value>
            </init-param>
        </filter>

        <!-- Filter mapping -->
        <filter-mapping>
            <filter-name>loggingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>

        <!-- Application events listener --> 
        <listener> 
            <listener-class>com.example.listener.ContextListener</listener-class> 
        </listener> 

        <!-- Session events listener -->
        <listener> 
            <listener-class>com.example.listener.SessionListener</listener-class> 
        </listener> 

        <!-- Security constraints -->
        <security-role> 
                <role-name>admin</role-name> 
        </security-role> 

        <security-role> 
                <role-name>editor</role-name> 
        </security-role> 
         
        <security-constraint> 
            <display-name>Limit access to the /private folder</display-name> 
            <web-resource-collection> 
                <web-resource-name>Protected directory</web-resource-name> 
                <url-pattern>/private/*</url-pattern> 
            </web-resource-collection> 

            <auth-constraint> 
                <role-name>admin</role-name> 
                <role-name>editor</role-name> 
            </auth-constraint> 
        </security-constraint> 

        <!-- Login configuration. BASIC auth required --> 
        <login-config> 
            <auth-method>BASIC</auth-method> 
            <realm-name>Editor Login</realm-name> 
        </login-config> 

        <!-- Transport guarantee. Options are: NONE, INTEGRAL and CONFIDENTIAL --> 
        <!-- INTEGRAL and CONFIDENTIAL requires SSL.                           -->
        <security-constraint> 
            <web-resource-collection> 
                <web-resource-name>Member area</web-resource-name> 
                <url-pattern>/member/*</url-pattern> 
            </web-resource-collection> 

            <user-data-constraint> 
                <transport-guarantee>CONFIDENTIAL</transport-guarantee> 
            </user-data-constraint> 
        </security-constraint> 

        <!-- Define an error handler for 404 pages --> 
        <error-page> 
            <error-code>404</error-code> 
            <location>/error404.jsp</location> 
        </error-page> 

        <!-- Define an error handler for java.lang.Throwable --> 
        <error-page> 
            <exception-type>java.lang.Throwable</exception-type> 
            <location>/errorThrowable.jsp</location> 
        </error-page> 

        <!-- Welcome files. -->
        <!-- Use index.jsp if available; if not, then index.html, etc. -->
        <welcome-file-list> 
            <welcome-file>index.jsp</welcome-file> 
            <welcome-file>index.html</welcome-file> 
            <welcome-file>index.htm</welcome-file> 
        </welcome-file-list> 

        <!-- Custom Tag Libraries --> 
        <!-- Taglib declarations are no longer required since JSP 2.0    -->
        <!-- If you do specify taglib here, the jsp-config parent tag is -->
        <!-- optional (taglib can be specified at the top level).        --> 
        <jsp-config> 
            <taglib> 
                <taglib-uri>customtags</taglib-uri> 
                <taglib-location>/WEB-INF/jsp/customtaglib.tld</taglib-location> 
            </taglib> 
        </jsp-config> 
</web-app>
Share |
| Comment  | Tags