Filters

A filter is a Java class that is invoked in response to a request for a resource in a Web application. Resources include Java Servlets, JavaServer pages (JSP), and static resources such as HTML pages or images. A filter intercepts the request and can examine and modify the response and request objects or execute other tasks.

When a filter intercepts a request, it has access to the javax.servlet.ServletRequest and javax.servlet.ServletResponse objects that provide access to the HTTP request and response, and a javax.servlet.FilterChain object. The FilterChain object contains a list of filters that can be invoked sequentially. When a filter has completed its work, the filter can either call the next filter in the chain, block the request, throw an exception, or invoke the originally requested resource.

Writing a Filter Class

To write a filter class, implement the javax.servlet.Filter interface. You must implement the following methods of this interface:

  • init()
  • destroy()
  • doFilter()

You use the doFilter() method to examine and modify the request and response objects, perform other tasks such as logging, invoke the next filter in the chain, or block further processing.

Configuring a Filter

To configure a filter:

  • Open the web.xml deployment descriptor in a text editor or use the Administration Console. For more information, see Web Application Developer Tools. The web.xml file is located in the WEB-INF directory of your Web application.

  • Add a filter declaration. The <filter> element declares a filter, defines a name for the filter, and specifies the Java class that executes the filter. The <filter> element must directly follow the <context-param> element and directly precede the <listener> and <servlet> elements. For example:

<context-param>Param</context-param>
<filter>
  <filter-name>myFilter</filter-name>
  <description>This is my filter</description>
  <filter-class>examples.myFilterClass</filter-class>
</filter>
<listener>Listener</listener>
<servlet>Servlet</servlet>
  • Add filter mappings. The <filter-mapping> element specifies which filter to execute based on a URL pattern or servlet name. The <filter-mapping> element must immediately follow the <filter> element(s).

  • To create a filter mapping using a URL pattern, specify the name of the filter and a URL pattern. URL pattern matching is performed according to the rules specified in the Servlet 2.3 Specification from Sun Microsystems, in section 11.1. For example, the following filter-mapping maps myFilter to requests that contain /myPattern/.

<filter-mapping>
  <filter-name>myFilter</filter-name>
  <url-pattern>/myPattern/*</url-pattern>
</filter-mapping>
  • To create a filter mapping for a specific servlet, map the filter to the name of a servlet that is registered in the Web application. For example, the following code maps the myFilter filter to a servlet called myServlet:
<filter-mapping>
  <filter-name>myFilter</filter-name>
  <servlet-hame>myServlet</servlet-name>
</filter-mapping>