Interceptors

An Interceptors is the object that intercepts an incoming request and performs operation before sending request to the target controller and perform some operations after receiving response from target controller.

An Interceptor is mapped with a URI pattern. All requests, matching URI pattern will be intercepted by the Interceptor. Multiple interceptors can be mapped with the single URI pattern.

Figure: Interceptor communication

You can create an Interceptor by two ways

  1. Implement HandlerInterceptor interface

  2. Inherit HandlerInterceptorAdapter abstract class. HandlerInterceptorAdapter implements HandlerInterceptor interface.

This HandlerInterceptor declares three methods:

  1. preHandle(..) is called before the actual target controller is executed

  2. postHandle(..) is called after the target controller is executed. and

  3. afterCompletion(..) is called after the complete request has executed.

The preHandle(..) method returns a boolean value. You can use this method to break or continue the processing of the interceptor execution chain. When this method returns true, the handler execution chain will continue; when it returns false, the DispatcherServlet stops further chain execution and transfers control to the desired page.

Here is an example interceptor TimeLoggerInt that intercepts incoming requests at URI pattern /UrlMapping/* and logs the pre and post-processing time-stamp.

public class TimeLoggerInt extends HandlerInterceptorAdapter {

/**

* Called before request is executed by target controller.

*/

@Override

public boolean preHandle(HttpServletRequest request,

HttpServletResponse response, Object handler) throws Exception {

System.out.println("TimeLoggerInt: preHandle() : " + new Date());

System.out.println("request uri is :: " + request.getRequestURI());

return true;

}

/**

* Called after request is executed by target controller.

*/

@Override

public void postHandle(HttpServletRequest request,HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

System.out.println("TimeLoggerInt: postHandle() ! " + new Date());

}

/**

* Called when request processing is completely done.

*/

@Override

public void afterCompletion(HttpServletRequest request,HttpServletResponse response, Object handler, Exception ex)

throws Exception {

System.out.println("TimeLoggerInt: after Completion() ! " + new Date());

}

}

Interceptor will be configured in configuration file with the help of <mvc:interceptor> tag.

Here in below example configuration mapping, Interceptor TimeLoggerInt will intercept all requests coming to URI pattern "/UrlMapping/*". Interceptor FrontCtlInt will intercept all incoming requests since it is mapped to "/*" URI Pattern.

<mvc:interceptors>

<mvc:interceptor>

<mvc:mapping path="/*" />

<bean class="in.co.sunrays.ctl.FrontCtlInt" />

</mvc:interceptor>

<mvc:interceptor>

<mvc:mapping path="/UrlMapping/*" />

<bean class="in.co.sunrays.ctl.TimeLoggerInt" />

</mvc:interceptor>

</mvc:interceptors>

Front Controller

This is the special kind of design pattern that intercepts all request and check authentication and authorization. It is responsible to stop request those are not authorized or authenticated.

Authentication is mainly to check whether user is logged in or not. If user is logged in then only allow user to access application functionality. Authorization is basically to provide role based access.

public class FrontCtlInt extends HandlerInterceptorAdapter {

@Override

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

HttpSession session = request.getSession();

if (session.getAttribute("user") == null) {

System.out.println("FrontCtlInt: User is not logged in ");

/*

* If user is not logged then forward it Login page and return false

* value to interceptor. I

*/

response.sendRedirect("Login.html");

return false;

}

return true;

}

//..

FAQ

Q: How do you create an interceptor?

Q: Which method will you override in interceptor?

Q: What are the difference between preHandle() and postHandle() methods?

Q: Can you map one interceptor to one controller?

Q: Can you map one interceptor to multiple controllers?

Q: Can you map multiple interceptors to one controller?

Q: Can you map multiple interceptors to multiple controllers?

Q: Why did you create an interceptor in your application?

A: We implemented Front Controller in our application using Interceptor. Front Controller does authentication and authorization for our application.