Struts 2 Interceptors Example

Interceptors are powerful mechanism to control the flow of each request. These are custom and default implementations which can be enforced to work as the callback methods at certain point of time. Struts 2 provides handful of default interceptors behind the scenes. One example is the exception handling at action level. Also we can write our own interceptors and add our own business logic inside the custom interceptors.

1. Action Class


public class Struts2HelloWorldAction{
	public String execute(){
		return "success";

2. Custom Interceptor


import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class LoggingInterceptor implements Interceptor{

	public void destroy() {
		System.out.println("Destroying logging interceptor...");

	public void init() {
		System.out.println("Initializing logging interceptor...");

	public String intercept(ActionInvocation invocation) throws Exception {
		String className = invocation.getAction().getClass().getName();
        long startTime = System.currentTimeMillis();
        System.out.println("Before calling action class: " + className);
        String result = invocation.invoke();
        long endTime = System.currentTimeMillis();
        System.out.println("After calling action class: " + className
                + " Time taken: " + (endTime - startTime) + " ms");
		return result;


3. Struts.xml

If you look at the interceptor definition, we have used interceptor-stack. It is the collection of interceptors defined in one block. The reason why we are doing is to add the default interceptors to the list, otherwise struts 2 will not invoke the default interceptors. 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts
Configuration 2.1//EN" "">
	<constant name="struts.devMode" value="true" />
	<package name="struts2demo" extends="struts-default">
			<interceptor name="logginginterceptor"
			<interceptor-stack name="loggingStack">
				<interceptor-ref name="logginginterceptor" />
				<interceptor-ref name="defaultStack" />
		<action name="InterceptorDemo" class="">
			<interceptor-ref name="loggingStack" />
			<result name="success">Result.jsp</result>

4. Result

If you access the application using URL http://localhost:8080/Struts2App/InterceptorDemo, you would see the following output in the console.

INFO: Choosing bean (struts) for (com.opensymphony.xwork2.util.TextParser)
Initializing logging interceptor...
15 Dec, 2013 5:52:02 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
15 Dec, 2013 5:52:02 PM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /
15 Dec, 2013 5:52:02 PM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/56  config=null
15 Dec, 2013 5:52:02 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1385 ms
Before calling action class:
After calling action class: Time taken: 64 ms

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!