Skip to main content

Intercepting Filter

ArchitecturalAPI designDecouplingLayered architecturePerformanceSecurityWeb developmentAbout 3 min


The Intercepting Filter pattern is intended to provide a pluggable framework for preprocessing and postprocessing web requests and responses. It allows different filters to process client requests and server responses in a configurable, decoupled manner.


Real-world Example

Consider entering a secure office building where you pass through several checkpoints: a security desk checks your ID, a metal detector ensures safety, and a registration desk logs your visit. Each checkpoint acts like a filter in the Intercepting Filter pattern, processing and validating your entry step-by-step, similar to how filters handle different aspects of web requests and responses in a software system.

In plain words

The Intercepting Filter design pattern allows you to define processing steps (filters) that execute sequentially to handle and modify web requests and responses before they reach the application or are sent to the client.

Wikipedia says

Intercepting Filter is a Java pattern which creates pluggable filters to process common services in a standard manner without requiring changes to core request processing code.

Programmatic Example

The Intercepting Filter design pattern is a Java EE pattern that creates pluggable filters to process common services in a standard manner without requiring changes to core request processing code. These filters can perform tasks such as authentication, logging, data compression, and encryption.

In the provided code, we can see an example of the Intercepting Filter pattern in the App, FilterManager, Client, and various Filter classes.

The App class is the entry point of the application. It creates an instance of FilterManager, adds various filters to it, and sets it to a Client.

public class App {

  public static void main(String[] args) {
    var filterManager = new FilterManager();
    filterManager.addFilter(new NameFilter());
    filterManager.addFilter(new ContactFilter());
    filterManager.addFilter(new AddressFilter());
    filterManager.addFilter(new DepositFilter());
    filterManager.addFilter(new OrderFilter());

    var client = new Client();

The FilterManager class manages the filters and applies them to the requests.

public class FilterManager {
  private final List<Filter> filters = new ArrayList<>();

  public void addFilter(Filter filter) {

  public void filterRequest(String request) {
    for (Filter filter : filters) {

The Client class sends the request to the FilterManager.

public class Client {
  private FilterManager filterManager;

  public void setFilterManager(FilterManager filterManager) {
    this.filterManager = filterManager;

  public void sendRequest(String request) {

The Filter interface and its implementations (NameFilter, ContactFilter, AddressFilter, DepositFilter, OrderFilter) define the filters that can be applied to the requests.

public interface Filter {
  void execute(String request);

public class NameFilter implements Filter {
  public void execute(String request) {
    // Implementation details...

// Other Filter implementations...

In this example, the App class sets up a FilterManager with various filters and assigns it to a Client. When the Client sends a request, the FilterManager applies all the filters to the request. This is a basic example of the Intercepting Filter pattern, where common processing tasks are encapsulated in filters and applied to requests in a standard manner.

Class diagram

Intercepting Filter
Intercepting Filter


Use the Intercepting Filter pattern when

  • Use the Intercepting Filter pattern when you need to apply pre-processing and post-processing steps to requests and responses, typically in web applications.
  • Suitable for handling cross-cutting concerns such as logging, authentication, data compression, and encryption transparently.


Known Uses



  • Promotes separation of concerns by allowing filters to be independently developed, tested, and reused.
  • Enhances flexibility through configurable filter chains.
  • Simplifies application maintenance by centralizing control in filter management.


  • Introducing many filters can lead to performance overhead due to the processing of each request and response through multiple filters.
  • Debugging and tracing the request flow through multiple filters can be complex.

Decoratoropen in new window: Filters in the Intercepting Filter pattern can be considered as decorators that add additional responsibilities to request handling. They modify the request/response without altering their fundamental behavior.
Chain of Responsibilityopen in new window: Filters are linked in a chain, where each filter processes the request or response and optionally passes it to the next filter in the chain, similar to how responsibilities are passed along in the Chain of Responsibility pattern.