Filters vs Interceptors in Java Web Apps: Key Differences Explained
This article compares Java web filters and Spring MVC interceptors, detailing their implementation differences, origins, execution order, supported project types, and typical use‑cases, helping developers choose the right tool for request handling, security, logging, and other cross‑cutting concerns.
Filters and interceptors are common tools in Java web development; this article outlines their main differences.
1. Implementation differences
Filters are implemented by creating a class that implements the Filter interface and overriding its methods:
<code>@WebFilter(urlPatterns = "/*")
public class MyTestFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("this is init()");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("=======start doFilter ===============");
filterChain.doFilter(servletRequest, servletResponse);
System.out.println("=======end doFilter =================");
}
@Override
public void destroy() {
System.out.println("=============== destroy ==========");
}
}
</code>Interceptors are implemented by creating a class that implements the HandlerInterceptor interface and providing its methods:
<code>@Component
public class MyTestInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("MyTestInterceptor=>preHandle ");
return true;
}
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
System.out.println("MyTestInterceptor=>postHandle ");
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
System.out.println("MyTestInterceptor=>afterCompletion ");
}
}
</code>2. Origin
Filters belong to the web server component (Servlet specification) and mainly filter servlet requests.
Interceptors belong to the Spring MVC framework and primarily intercept controller requests.
3. Execution order
When a request arrives, it first passes through the servlet container’s filters, then reaches the DispatcherServlet , which invokes the interceptor before delegating to the controller. After processing, the response follows the reverse chain.
4. Supported project types
Filters are defined by the servlet specification and require a servlet container, so they are limited to web projects. Interceptors are Spring components managed by the Spring container, not dependent on Tomcat, and can be used in web applications as well as other Java programs such as Swing or standalone applications.
5. Typical use cases
Interceptors, being closer to the controller layer, are often used for business‑level infrastructure tasks such as login checks, permission validation, logging, view rendering, request parameter handling, and URL redirection.
Filters are typically used for generic cross‑cutting concerns like sensitive word filtering, character encoding, response compression, authentication and authorization, performance monitoring, CORS handling, and logging.
Summary
Implementation: Filters use callback methods; interceptors use dynamic proxies (reflection).
Granularity: Interceptors can intercept at a finer level than filters.
Dependency: Filters depend on a servlet container; interceptors depend on the Spring framework.
Execution order: Filters run before and after the servlet; interceptors run before and after the handler.
Lobster Programming
Sharing insights on technical analysis and exchange, making life better through technology.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.