ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Writing Servlet 2.3 Filters
Pages: 1, 2, 3, 4

The web.xml to deploy this filter and define initial parameters is shown in Listing 7.


Listing 7: The deployment descriptor that defines initial parameters for a filter. (web.xml)



<?xml version = "1.0" encoding = "ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">

<web-app>

  <!-- Define the filters within the Web Application -->

  <filter>
    <filter-name>
      Parsing Request Data
    </filter-name>
    <filter-class>
      com.filters.RequestFilter
    </filter-class>

    <init-param>
      <param-name>
        User's Name
      </param-name>
      <param-value>
        myname
      </param-value>
    </init-param>

    <init-param>
      <param-name>
        User's Email
      </param-name>
      <param-value>
        txtemail
      </param-value>
    </init-param>
  </filter>


  <!-- Map the filter to a Servlet or URL -->

  <filter-mapping>
    <filter-name>
      Parsing Request Data
    </filter-name>
    <url-pattern>
      /formprocessor
    </url-pattern>
  </filter-mapping>



  <!-- Define the Servlets within the Web Application -->

  <servlet>
    <servlet-name>
      No Form Submitted
    </servlet-name>
    <servlet-class>
      com.servlets.NoElements
    </servlet-class>
  </servlet>

  <servlet>
    <servlet-name>
      Empty Form Elements
    </servlet-name>
    <servlet-class>
      com.servlets.InvalidEntries
    </servlet-class>
  </servlet>

  <servlet>
    <servlet-name>
      Good Request
    </servlet-name>
    <servlet-class>
      com.servlets.MyServlet
    </servlet-class>
  </servlet>


  <!-- Define Servlet mappings to urls -->

  <servlet-mapping>
    <servlet-name>
      No Form Submitted
    </servlet-name>
    <url-pattern>
      /noform
    </url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>
      Empty Form Elements
    </servlet-name>
    <url-pattern>
      /emptyform
    </url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>
      Good Request
    </servlet-name>
    <url-pattern>
      /formprocessor
    </url-pattern>
  </servlet-mapping>

</web-app>


Summary

The Servlet 2.3 API specification provided important new features. Application lifecycle events provide the developer better control over the ServletContext object and HttpSession objects. Filters provide a way to take control logic out of servlets, placing it in more reusable pieces of code.

Stephanie Fesler is a BEA Systems expert on implementing various Java 2EE API.

Related Articles:

Servlet App Event Listeners

Servlet 2.3 API Features Exposed


Return to ONJava.com.