1. What is a HandlerInterceptor used for in Spring MVC?
Why: A HandlerInterceptor lets you run custom code before and after a controller (handler) method is called.
2. Which HandlerInterceptor method is called before the controller method is invoked?
Why:preHandle runs before the target handler method is called. postHandle and afterCompletion run later. beforeHandle does not exist.
3. What happens when the preHandle method returns false?
Why: Returning false from preHandle means the request processing is abandoned. The handler method is not invoked.
4. When is the postHandle method called?
Why:postHandle runs after the handler has executed and before the view is rendered. This is why it can still change the model.
5. Which method of HandlerInterceptor is a good place for resource cleanup?
Why:afterCompletion is called after the view is rendered. It is a good place to release resources.
6. What should replace the blank so that this class works as an interceptor?
public class AuditInterceptor ________ {
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception {
return true;
}
}
Why:HandlerInterceptor is an interface, so a class must use implements to become an interceptor.
7. Which line should be added inside the method to register the interceptor?
@Configuration
@EnableWebMvc
public class MyWebConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
// add code here
}
}
Why: The InterceptorRegistry has an addInterceptor method. It is used to register an interceptor in a WebMvcConfigurer implementation.
8. The interceptor below is registered. A user opens /home and the view is rendered successfully. What is the printed output order?
public class MyInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest req,
HttpServletResponse res, Object handler) {
System.out.println("A");
return true;
}
public void postHandle(HttpServletRequest req,
HttpServletResponse res,
Object handler, ModelAndView mv) {
System.out.println("C");
}
public void afterCompletion(HttpServletRequest req,
HttpServletResponse res, Object handler,
Exception ex) {
System.out.println("D");
}
}
@Controller
public class HomeController {
@GetMapping("/home")
public String home() {
System.out.println("B");
return "home";
}
}
Why: The order is: preHandle (A), the controller method (B), postHandle (C), then afterCompletion (D).
9. The interceptor below is registered. A user opens /hello. What is printed?
public class BlockInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest req,
HttpServletResponse res, Object handler) {
System.out.println("A");
return false;
}
}
@Controller
public class HelloController {
@GetMapping("/hello")
public String hello() {
System.out.println("B");
return "hello";
}
}
Why:preHandle returns false, so the controller method is never called. Only "A" is printed.
10. Which statement correctly compares a Servlet Filter with a HandlerInterceptor?
Why: A Servlet Filter can exchange the request and response objects that are passed down the chain. A HandlerInterceptor can only do pre-processing and post-processing around the handler.
11. Which task is the best fit for a HandlerInterceptor?
Why: Interceptors are good for handler-related tasks such as timing, logging, and common model attributes. GZIP, multipart handling, and wrapping the request are better done with filters.
12. Why can a class implement HandlerInterceptor and override only the methods it needs?
Why: Since Spring 5.0, HandlerInterceptor uses Java 8 default methods. You only need to override the methods you want.
13. What happened to HandlerInterceptorAdapter in Spring 6?
Why:HandlerInterceptorAdapter was deprecated in Spring 5.3 and removed completely in Spring 6. It is no longer needed because HandlerInterceptor has default methods.
14. Why does the postHandle code below check modelAndView for null?
Why: The ModelAndView parameter is nullable. For some requests there is no model and view to work with, so the code must check before using it.
15. The interceptor below is registered. A controller method returns a view that contains the JSP line shown. What text does the page show inside the paragraph?
Why:postHandle runs before the view is rendered, so the attribute added to the model is available to the view. The page shows "Hi".
16. Which kind of work are interceptors best kept for?
Why: Interceptors are best for cross-cutting technical concerns like logging, timing, and setting common model attributes. They are not meant to be a security layer.