Interceptor in Spring 3 MVC

Interceptors are very important to a web framework/application. MVC frameworks like Struts 2 implements most of the framework’s functionality via Interceptors.

In general intercepts are used for the purpose of implemeting User Authentication, Logging, Hibernate session management via “Open Session in View” design pattern etc. In Spring 3 we can add an interceptor by implementing HandlerInterceptor interface.

package com.bharat.spring.mvc.interceptors;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class LoggingInterceptor implements HandlerInterceptor{

    private Logger log = Logger.getLogger(this.getClass());

    public void afterCompletion(HttpServletRequest request,
            HttpServletResponse response, Object object, Exception exception)
            throws Exception {"This gets executed after Completion.");


    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
            Object arg2, ModelAndView arg3) throws Exception {"This method gets executed after our request is handled.");


    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
            Object object) throws Exception {"*******************************************************");"** Remote Address : " + request.getRemoteAddr());"** Context Path    : " + request.getContextPath());"** Request URI    : " + request.getRequestURI());
        Enumeration<String> attrs = request.getAttributeNames();
            String attr = attrs.nextElement();
  "** Request Attr    : " + attr +" = "+ request.getAttribute(attr));
        }"** Request URI    : " + request.getAttributeNames());"*******************************************************");
        return true;


In your application’s Spring config file this is configured as below. In my example it is dispatcher-servlet.xml

<beans xmlns=""
	xmlns:xsi="" xmlns:mvc=""

	<!-- Interceptors -->
		<bean id="loggingInterceptor" class="com.bharat.spring.mvc.interceptors.LoggingInterceptor" />
		<!-- More interceptors go here -->

The order of execution of interceptors is the order in which you put
between your <mvc:interceptors>…</mvc:interceptors> tag.

web.xml of you application can be configured like below.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=""
	xmlns="" xmlns:web=""
	id="WebApp_ID" version="2.5">
	<!-- Log4j config listener -->
	<!-- Spring security filter -->
	<!--Used to Enable Spring MVC -->

, , ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: