Close
« Tutorial

Spring MVC - HandlerInterceptor MCQ

0/13
0 ✓
0 ✗

1. What is a HandlerInterceptor used for in Spring MVC?

2. Which HandlerInterceptor method is called before the controller method is invoked?

3. What happens when the preHandle method returns false?

4. When is the postHandle method called?

5. Which method of HandlerInterceptor is a good place for resource cleanup?

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;
    }
}

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
    }
}

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";
    }
}

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";
    }
}

10. Which statement correctly compares a Servlet Filter with a HandlerInterceptor?

11. Which task is the best fit for a HandlerInterceptor?

12. Why can a class implement HandlerInterceptor and override only the methods it needs?

13. What happened to HandlerInterceptorAdapter in Spring 6?

14. Why does the postHandle code below check modelAndView for null?

public void postHandle(HttpServletRequest request,
                       HttpServletResponse response,
                       Object handler,
                       ModelAndView modelAndView) throws Exception {
    if (modelAndView != null) {
        modelAndView.addObject("info", "Hello");
    }
}

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?

public void postHandle(HttpServletRequest request,
                       HttpServletResponse response,
                       Object handler,
                       ModelAndView modelAndView) throws Exception {
    if (modelAndView != null) {
        modelAndView.addObject("msg", "Hi");
    }
}

JSP line:
<p>${msg}</p>

16. Which kind of work are interceptors best kept for?

Join