Intercepting Filter

Brief Description

Most applications have some requirements, such as security and logging, that are applicable across all application requests. To add such functionality separately to each application service would be time-consuming, error-prone, and difficult to maintain. Even implementing these services within a front controller would still require code changes to add and remove services. The sequence diagram in Figure 1 below shows how each Web resource is responsible for calling such services individually.

Figure 1. Before Intercepting Filter

The Intercepting Filter pattern wraps existing application resources with a filter that intercepts the reception of a request and the transmission of a response. An intercepting filter can pre-process or redirect application requests, and can post-process or replace the content of application responses. Intercepting filters can also be stacked one on top of the other to add a chain of separate, declaratively-deployable services to existing Web resources with no changes to source code. Figure 2 below shows a chain of two intercepting filters intercepting requests to two Web resources that they wrap.

Figure 2. After Intercepting Filter

Detailed Description

See the Core J2EETM Patterns

Detailed Example

The Servlet Filter interface in the JavaTM 2 Platform, Enterprise Edition (J2EE) platform is a direct implementation of the Intercepting Filter pattern. The sample application defines two servlet filters: one that mediates customer signon, and another that enforces response encoding.


Copyright © 2002 Sun Microsystems, Inc. All Rights Reserved.