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 filters in a web application are defined in the deployment descriptor of the web application, the web.xml file. Filters are defined and then mapped to a URL or Servlet, in much the same was as Servlet is defined and then mapped to a URL pattern. Listing 2 shows the web.xml file that deployed the SimpleFilter.java from Listing 1, mapping it to a Servlet.


Listing 2: The deployment descriptor for the simple 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>
      Simple Filter Example
    </filter-name>
    <filter-class>
      com.filters.SimpleFilter
    </filter-class>
  </filter>


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

  <filter-mapping>
    <filter-name>
      Simple Filter Example
    </filter-name>
    <url-pattern>
      /simple
    </url-pattern>
  </filter-mapping>


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

  <servlet>
    <servlet-name>
      Simple Servlet
    </servlet-name>
    <servlet-class>
      com.servlets.SimpleServlet
    </servlet-class>
  </servlet>


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

  <servlet-mapping>
    <servlet-name>
      Simple Servlet
    </servlet-name>
    <url-pattern>
      /simple
    </url-pattern>
  </servlet-mapping>

</web-app>

Figure 2 shows the console of the Tomcat server where this web application was running after the servlet had handled a request.

Screen shot.
Figure 2. Console of Tomcat running the simple filter.

Filter Chaining

In the first example only one filter that handled the request and response for the simple servlet. What if you want multiple filters to handle the request and response? Figure 3 shows what a filter chain looks like.

Diagram of filter chaining.
Figure 3. A diagram of filter chaining.

Multiple filters can be written and applied to the same URL pattern. The order of execution is determined by the ordering in the deployment descriptor. Remember that in the doFilter() method you call the next element in the chain with

chain.doFilter(request, response)

Diagram of the filter-chaining example.
Figure 4. Diagram of the filter-chaining example.

Listing 3 shows the code for Filter1.java, which is a simple filter that outputs to the console of the web server a message. The message allows you to track the request through the filters.


Listing 3: A simple filter that outputs a message to the console. (Filter1.java)

package com.filters;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import java.io.IOException;
import javax.servlet.ServletException;

public class Filter1 implements Filter
{
  private FilterConfig filterConfig;

  public void doFilter (ServletRequest request,
              ServletResponse response,
              FilterChain chain)
  {

    try
    {
      System.out.print ("Within First Filter ... ");
      System.out.println ("Filtering the Request ...");

      chain.doFilter (request, response);

      System.out.print ("Within First Filter ... ");
      System.out.println ("Filtering the Response ...");

    } catch (IOException io) {
      System.out.println ("IOException raised in Filter1 Filter");
    } catch (ServletException se) {
      System.out.println ("ServletException raised in Filter1 Filter");
    }
  }

  public FilterConfig getFilterConfig()
  {
    return this.filterConfig;
  }

  public void setFilterConfig (FilterConfig filterConfig)
  {
    this.filterConfig = filterConfig;
  }
}


The second filter prints a message to the console of the web server to help us track the request and response. Listing 4 shows the code for the second filter in the chain.


Listing 4. A simple filter that outputs a message to the console. (Filter2.java)

package com.filters;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import java.io.IOException;
import javax.servlet.ServletException;

public class Filter2 implements Filter
{
  private FilterConfig filterConfig;

  public void doFilter (ServletRequest request,
              ServletResponse response,
              FilterChain chain)
  {

    try
    {
      System.out.print ("Within Second Filter ... ");
      System.out.println ("Filtering the Request ...");

      chain.doFilter (request, response);

      System.out.print ("Within Second Filter ... ");
      System.out.println ("Filtering the Response ...");

    } catch (IOException io) {
      System.out.println ("IOException raised in Filter2 Filter");
    } catch (ServletException se) {
      System.out.println ("ServletException raised in Filter2 Filter");
    }
  }

  public FilterConfig getFilterConfig()
  {
    return this.filterConfig;
  }

  public void setFilterConfig (FilterConfig filterConfig)
  {
    this.filterConfig = filterConfig;
  }
}


Pages: 1, 2, 3, 4

Next Pagearrow