What the pattern is trying to achieve
Create a configurable pipeline of reusable request-processing steps that run before or around the main handler.
Intercepting Filter creates a reusable pipeline for cross-cutting request concerns so individual handlers do not each own authentication, logging, normalization, or tracing logic.
In order management flows, checkout and account operations often need security checks, locale setup, correlation IDs, and auditing. Filters let those concerns run consistently before the business handler.
Create a configurable pipeline of reusable request-processing steps that run before or around the main handler.
Cross-cutting request logic gets duplicated across controllers, servlets, or endpoints.
Arrange reusable filters that inspect or modify the request and then delegate forward through a chain.
The pattern remains highly relevant in servlet stacks, Spring Security, API gateways, and observability pipelines.
package org.javaomnibus.ecommerce.web;
import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import java.io.IOException;
import java.util.Locale;
import java.util.UUID;
public final class CorrelationFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
request.setAttribute("correlationId", UUID.randomUUID().toString());
request.setAttribute("locale", Locale.US);
chain.doFilter(request, response);
}
}
Use Intercepting Filter for reusable infrastructure concerns. Use Front Controller for central request routing and high-level web coordination.