Spring MVC - Servlet Filter vs Handler Interceptor Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Filter is a Java class that is executed by the Servlet Container for each incoming HTTP request and each HTTP response. Filter is associated with the Servlet API, while HandlerIntercepter is unique to Spring. Only after Filters will Interceptors begin to operate. HandlerInterceptors are appropriate for fine grained pre-processing duties (authorization checks, etc.). Spring MVC (Servlet) FilterBelow are the Servlet Filter methods. ServletFilter methods:destroy(): When the filter is removed from the service, the destruct() function is only called once.init(FilterConfig config): This function, init(FilterConfig config), is only used once. It serves as the filter's initialization.doFilter method (): Every time a user sends a request to any resource that the filter is mapped to, the doFilter method () is called. It is applied to jobs involving filtration.Setting Up a (Servlet)FilterFirst, we need to develop a class that implements the javax.servlet in order to establish a filter.Interface for filters: Java import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import java.io.IOException; @Component public class LogFilter implements Filter { private Logger logger = LoggerFactory.getLogger(LogFilter.class); @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // Log information about the incoming request logger.info("Hello from: " + request.getLocalAddr()); // Continue with the filter chain chain.doFilter(request, response); } // Other methods of the Filter interface (init, destroy) can be implemented if needed. } Spring MVC (Handler) InterceptorBelow are the HandlerInterceptor methods. HandlerInterceptor Methods:preHandle(): Runs before to calling the target handler.postHandle(): This function is called after the target handler but before the view is rendered by the DispatcherServlet.afterCompletion(): Callback when request processing and display rendering being finished.Create a HandlerInterceptorThe org.springframework.web.servlet.HandlerInterceptor interface must be implemented by a class before it can be used as a HandlerInterceptor. LogFilter: Java @Component public class LogFilter implements Filter { private Logger logger = LoggerFactory.getLogger(LogFilter.class); @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { logger.info("Hello from: " + request.getLocalAddr()); chain.doFilter(request, response); } } LogInterceptor: Java public class LogInterceptor implements HandlerInterceptor { private Logger logger = LoggerFactory.getLogger(LogInterceptor.class); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { logger.info("preHandle"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { logger.info("postHandle"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { logger.info("afterCompletion"); } } Difference between (Servlet) Filter and (Handler) InterceptorCharacteristics (Servlet) Filter (Handler) Interceptor Definition Every time an HTTP request or answer is received, the servlet container runs the Java class Filter. An interceptor only permits custom post-processing with access to Spring Context and custom pre-processing with the possibility to forbid the handler's execution. Interface jakarta.servlet.Filter HandlerInterceptor Execution order Prior to/after servlets, servlet filters Prior to or following controller technique, spring interceptorswon't start operating until after Filters. Level of operation Filter operates on Servlet level. Interceptor operates on Controller level. Method Compared to Interceptor's postHandle, Filter's doFilter technique is far more flexible. It is possible to modify the request or answer, forward it down the chain, or even stop the request from being processed. Since you have access to the real target "handler," a HandlerInterceptor provides more precise control than a filter. Even the handler method's annotation status may be verified. Comment More infoAdvertise with us Next Article Spring MVC Interview Questions and Answers A aritrikghosh784 Follow Improve Article Tags : Advance Java Dev Scripter Java-Spring-MVC Dev Scripter 2024 Similar Reads Spring MVC - HandlerInterceptor Spring interceptor is functional, so let's take a step back and examine the handler mapping. The HandlerInterceptor interface is a handy class offered by Spring. We may override only the relevant methods out of the three by extending this. The HandlerInterceptor interface is implemented by the Handl 3 min read Spring Security - Find the Registered Filters In Spring Boot, Spring Security is the most powerful and customizable authentication and access control framework for Java applications, and it provides strong security features to protect web applications from various security threats such as authentication, authorization, session management, and w 14 min read How to Autowire a Spring Bean in a Servlet Filter? In Spring Boot, filters can process incoming requests before they reach the controller and modify responses before sending them to the client. We often need to inject (autowire) Spring-managed beans into filters to access services or configurations. Spring does not manage servlet filters by default, 6 min read Spring MVC Interview Questions and Answers Spring MVC is a powerful Java framework for building web applications. It is known for its complete configuration options, seamless integration with other Java frameworks, and robust architecture. Many top companies, including Netflix, Amazon, Google, and Airbnb, rely on Spring MVC to build scalable 15 min read Spring Boot - Servlet Filter Spring boot Servlet Filter is a component used to intercept & manipulate HTTP requests and responses. Servlet filters help in performing pre-processing & post-processing tasks such as: Logging and Auditing: Logging operations can be performed by the servlet filter logging the request and res 8 min read Get the Response Body in Spring Boot Filter In a Spring Boot application, filters can be used to intercept requests and responses, allowing you to manipulate them before reaching the controller or after the response is generated. A common requirement is capturing or logging the response body in the Spring Boot filter, especially for monitorin 5 min read Like