Fading Coder

One Final Commit for the Last Sprint

Home > Notes > Content

Servlet Filters vs Spring MVC Interceptors: Distinctions and Execution Flow

Notes 1

Core Distinctions

  • Interceptors leverage Java reflection, whereas Filters rely on function callbacks.
  • Filters are tightly coupled with the Servlet container, while Interceptors are independent of it and exist with in the Spring framework.
  • Filters can intercept almost any request type (including static resources), but Interceptors only target requests handled by the DispatcherServlet.
  • Interceptors have access to the Spring MVC context and handler-specific objects like ModelAndView, which Filters cannot access.
  • Interceptors can seamlessly inject and interact with Spring-managed beans (e.g., Service layers) via IoC, whereas Filters require explicit integration with the Spring application context.
  • Filters are instantiated once by the container and reused across requests, whereas Interceptors are managed by Spring and can be invoked multiple times during the request processing lifecycle.

Configuration & Implementation

Interceptors are registered within the Spring MVC configuration, specifying included and excluded path patterns:

@Configuration
public class WebInterceptorConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new SecurityInterceptor())
                .addPathPatterns("/api/items/**", "/api/accounts/**")
                .excludePathPatterns("/api/accounts/login");
    }
}

Filters operate at the Servlet container level, executing logic before and after the request passes through the filter chain:

public class RequestLoggingFilter implements Filter {
    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("Filter: Incoming request logged");
        filterChain.doFilter(req, res);
        System.out.println("Filter: Outgoing response logged");
    }
}

Interceptors wrap around the actual handler execution, providing distinct callback points:

public class SecurityInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object handler) throws Exception {
        System.out.println("Interceptor: preHandle execution");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest req, HttpServletResponse res, Object handler, ModelAndView mav) throws Exception {
        System.out.println("Interceptor: postHandle execution");
    }

    @Override
    public void afterCompletion(HttpServletRequest req, HttpServletResponse res, Object handler, Exception ex) throws Exception {
        System.out.println("Interceptor: afterCompletion execution");
    }
}

Execution Flow and Nesting

Filters encapsulate Interceptors. The filterChain.doFilter(req, res) invocation acts as the boundary. When this method is called, control transitions into the Servlet container, eventually reaching the DispatcherServlet, which triggers the Interceptor chain.

  1. preHandle executes after the initial Filter logic but before the target Controller method is invoked.
  2. postHandle executes after the Controler method returns but before the view is rendered, alowing modifications to the ModelAndView.
  3. afterCompletion executes after view rendering is complete, but still within the boundary of filterChain.doFilter().
  4. Once afterCompletion finishes, the DispatcherServlet completes its service, and the call stack returns to the filterChain.doFilter() line in the Filter, executing the remaining post-processing logic.

The overall sequence is: Filter pre-processing -> DispatcherServlet -> Interceptor preHandle -> Controller -> Interceptor postHandle -> View Rendering -> Interceptor afterCompletion -> DispatcherServlet exit -> Filter post-processing.

Related Articles

Designing Alertmanager Templates for Prometheus Notifications

How to craft Alertmanager templates to format alert messages, improving clarity and presentation. Alertmanager uses Go’s text/template engine with additional helper functions. Alerting rules referenc...

Deploying a Maven Web Application to Tomcat 9 Using the Tomcat Manager

Tomcat 9 does not provide a dedicated Maven plugin. The Tomcat Manager interface, however, is backward-compatible, so the Tomcat 7 Maven Plugin can be used to deploy to Tomcat 9. This guide shows two...

Skipping Errors in MySQL Asynchronous Replication

When a replica halts because the SQL thread encounters an error, you can resume replication by skipping the problematic event(s). Two common approaches are available. Methods to Skip Errors 1) Skip a...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.