HandlerInterceptor vs Servlet Filter
Fast recall
Quick revision
Core answer
Knowledge of Spring MVC's internal extension points and ability to pick the right tool for cross-cutting concerns.
Implementing everything as Filters because they are more familiar, missing the richer context available to interceptors and the ability to abort after handler resolution.
Mention that Filters are better for concerns that must apply to all URLs (including static resources, actuator endpoints) or that need to wrap the request/response stream, such as request body caching for logging.
Interview-ready guide
Interview answer
Filters and interceptors both intercept HTTP requests, but they operate at different layers of the stack. The choice determines what context you have access to and what requests you can affect.
Filters (Servlet layer)
- Part of the Servlet spec; execute in a chain before
DispatcherServlet - Access to raw
HttpServletRequestandHttpServletResponseonly — no Spring context - Apply to every request matching the URL pattern, including static resources
- Use for: authentication (Spring Security), encoding (
CharacterEncodingFilter), CORS, request/response logging at the raw HTTP level
HandlerInterceptors (Spring MVC layer)
- Registered with Spring MVC's
HandlerMapping; execute insideDispatcherServletafter handler resolution preHandle(request, response, handler)receives the resolvedHandlerMethod— inspect controller annotations, method names, class types- Only fire for requests that map to a handler — static resources and 404s do not trigger interceptors
- Use for: audit logging with handler context,
@RequiresPermissionannotation checks, user locale/timezone setup
`afterCompletion` is the cleanup hook
- Fires after the response is committed, even when an exception was thrown
- Correct place to remove
MDCentries, release thread-local resources, or record timing metrics
HandlerInterceptor vs Servlet Filter
Direct Answer
A Servlet Filter operates at the Servlet container level and runs before DispatcherServlet, with no access to Spring's context. A HandlerInterceptor runs inside DispatcherServlet's pipeline with full access to the handler (controller method) and ModelAndView, making it the right choice for Spring-specific concerns like authentication, logging with request metadata, or modifying the model before rendering.
Common Mistakes & Pitfalls
Implementing everything as Filters because they are more familiar, missing the richer context available to interceptors and the ability to abort after handler resolution.
Check your understanding
Answer these without looking back at the article.
- How would you implement a HandlerInterceptor that reads a custom annotation on the controller method to enforce rate limiting?
- Why does Spring Security use Filters rather than HandlerInterceptors?
- How do you register a HandlerInterceptor in a Spring Boot application?
Related interview prep categories
1Want a guided curriculum order?
These modules are also sequenced in our flagship backend interview track — open it when you prefer an ordered path instead of browsing by topic.