What are the methods in the HandlerInterceptor interface?

HandlerInterceptor defines three lifecycle hook methods that let you run custom logic at different points around a controller method's execution within the DispatcherServlet request cycle.

Key Points

Example

A logging interceptor might record the start time in preHandle(), then in afterCompletion() compute and log the total request duration, giving per-request timing without touching any controller code.

public class LoggingInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object handler) {
        req.setAttribute("startTime", System.currentTimeMillis());
        return true;
    }
    public void afterCompletion(HttpServletRequest req, HttpServletResponse res, Object handler, Exception ex) {
        long duration = System.currentTimeMillis() - (long) req.getAttribute("startTime");
        System.out.println("Request took " + duration + "ms");
    }
}

Interview Tip

A concise interview answer is

HandlerInterceptor has three hooks: preHandle before the controller runs, where returning false short-circuits the request; postHandle after the controller but before the view renders, for tweaking the model; and afterCompletion after rendering, for cleanup or logging, which fires even on exceptions.

Last updated: 16 July 2026

Previous question: How are interceptors used in Spring MVC?

Next question: How can you configure a Spring MVC interceptor to apply globally?

More Spring MVC (Optional) interview questions

All Spring MVC (Optional) questions