HandlerInterceptor
In general, in web applications, to provide pre-processing and post-processing services for web resources like Servlets and JSPs we will use “Servlet Filters”.
In web applications, we are able to provide single filter for multiple Servlets and JSPs and we are able to provide multiple filters for Single Servlet or JSP.
In the above representation, when we submit request to the container for a particular Servlet then container will perform the following actions.
- Container will check whether any Filter or Filters are associated with the respective Servlet or not.
- If any Filter or Filters are associated with the Servlet then container will execute all the Filters in one by one fashion in an order which we configured in web.xml file.
- If the present request is satisfying Filter constraints then Container will forward request to the next filter or to the Servlet if no filter is existed in next.
- If the present request is not satisfying the Filter constraints then Filter will send response to the client through all the previous filters.
- If all the Filters are executed then Container will execute the respective Servlet, after execution of the Servlet Container will send the generated response to the client through all the Filters in reverse order.
Similarly in Spring Web MVC applications, if we want to provide Pre-Processing and Post-Processing services to any controller or all the controllers components then we have to use Interceptors.
If we provide Interceptors in Spring WEB MVC applications then they will be executed before executing the controller, after executing the controller and then after sending response to client.
If we provide Interceptors in Spring WEB MVC applications then they will be executed before executing the controller, after executing the controller and before sending response to client and then after sending response to client.
If we want to use Interceptors in Spring WEB MVC applications then we have to use the following two steps.
- Prepare Interceptor class
- Configure Interceptor class in Spring configuration File.
1. Prepare Interceptor class
To prepare Interceptor class we have to declare an user defined class and either implement org.springframework.web.servlet.HandlerInterceptor interface or extend org.springframework.web.servlet.handler.Handler-InterceptorAdapter class in User defined class.
Where HandlerInterceptor is an interface contains the following three methods.
public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler)throws Exception
- This method will include pre-processing logic which we want to execute before executing controller method
- This method will be executed before executing controller class.
- This method will return a boolean value like true or false.
- If this method returns ‘true’ value then DispatcherServlet will understand the present Interceptor is executed successfully and request will be forwarded to the next interceptor or to the Controller if no interceptor is existed.
- If this method returns ‘false’ value then the present Interceptor constraints are not satisfied by the present request and Interceptor generates the required response to the client through all the previous interceptors with out executing the respective Controller.
public void postHandle( HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception
- This method will include the Post-Processing services logic which we want to execute after executing the controller component.
- This method will be executed after executing the controller component and before submitting response to client.
- This method can be used to add additional attribute to the ModelAndView object to be used in the view pages.
- Thismethod can be used to determine the time taken by handler method to process the client request.
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) throws Exception
- This method will be executed after executing the controller class and after sending response to client.
- This method will be executed only once for all the controller classes, that is, after the response rendering.
Note
In Spring WEB MVC applications, preHandler() and postHandle() methods are executed for each and every controller, but, afterCompletion() method will be executed only once after the response rendering.
If more than one Interceptor is provided for a controller component then all the Interceptors are executed like below.
If we provide Multiple Interceptors in Spring WEDB MVC application then preHandle() methods will be executed as per the Interceptors configuration order which we configured in Spring configuration file, but, postHandle() and afterCompletion() methods are executed in reverse order of the Interceptors.
2. Configure Interceptor class in Spring configuration File
To configure Interceptor class in Spring configuration file , we have to use the following approaches.
- Use “interceptors” property of type list in HandlerMapping configuration
- User tag in spring configuration file directly.
- Use tag in Spring configuration file.
1. Use “interceptors” property of type list in HandlerMapping configuration
<beans> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="interceptors"> <list> <ref bean="maintenanceInterceptor" /> <ref bean="executeTimeInterceptor" /> </list> </property> </bean> ---- </beans>
2. User <interceptors> tag in spring configuration file directly
<beans> ---- <interceptors> <interceptor> <mapping path="/home"/> <beans:bean class="com.ashok.spring.interceptors.RequestProcessingTimeInterceptor"></beans:bean> </interceptor> </interceptors> ------ </beans>
3. Use tag <mvc:interceptors> in Spring configuration file
<beans> ----- <mvc:interceptors> <bean class="com.javapapers.spring.mvc.interceptor.GreetingInterceptor" /> <mvc:interceptor> <mvc:mapping path="/AnimalList" /> <bean class="com.ashok.interceptor.AnimalInterceptor" /> </mvc:interceptor> </mvc:interceptors> ----- </beans>