Configuration File Example
Below is a simple context web.xml file that demonstrates all of the basic elements discussed in this chapter:

definition of a servlet, a filter that processes requests for the servlet, and a listener

initiation parameters

error page definition

session configuration

URL mapping
Each of these items is described in detail in the remainder of this chapter.
 
<web-app>
 
  <!-- Define and map a servlet -->
  <servlet>
   <servlet-name>HelloWorld</servlet-name>
   <servlet-class>
     servletexample.createHelloWorldExample</servlet-class>
    <init-param>
      <param-name>greeting</param-name>
      <param-value>Hello, World!</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
   <servlet-name>HelloWorld</servlet-name>
   <url-pattern>/HelloWorldExample/*</url-pattern>
  </servlet-mapping>
 
  <!-- Define and map a filter -->
  <filter>
    <filter-name>simpleFilter</filter-name>
    <filter-class>servletexample.createSimpleFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>simpleFilter</filter-name>
    <url-pattern>/HelloWorldExample/*</url-pattern>
  </filter-mapping>
 
  <!-- Define a listener -->
  <listener>
    <listener-class>mylistener.createSessionListener</listener-class>
  </listener>
 
<!-- Define an error-page for code 404, File Not Found -->
  <error-page>
    <error-code>404</error-code>
    <location>404.html</location>
  </error-page>
 
  <!-- Define a 10 minute session timeout -->
  <session-config>
    <session-timeout>10</session-timeout>
  </session-config>
 
</web-app>
A production application generally has a more elaborate configuration file. A file can contain any number of servlet, filter, and listener declarations. A file may contain one session-config element, and one error-page element for each HTTP error code.