Create and Map a Custom Interceptor
Interceptors are a core feature in Struts2 that allow developers to perform processing before and after an action is executed. They can be used for various purposes, such as logging, authentication, and input validation. While Struts2 provides several built-in interceptors, there may be cases where you need to create a custom interceptor to meet specific requirements.

Table of Contents
Steps to Create and Map a Custom Interceptor:
- 1. Â Create the Interceptor Class : Implement the Interceptor interface or extend AbstractInterceptor.
- 2. Â Configure the Interceptor : Define the interceptor in the struts.xml configuration file.
- 3. Â Map the Interceptor to an Action : Apply the custom interceptor to specific actions or packages in struts.xml.
Java Example
Let’s create a custom interceptor that logs the execution time of an action.
Step 1: Create the Custom Interceptor
java
package com.example.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class ExecutionTimeInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
long startTime = System.currentTimeMillis();
String result = invocation.invoke();
long endTime = System.currentTimeMillis();
System.out.println("Action executed in " + (endTime - startTime) + " milliseconds");
return result;
}
}
Step 2: Configure the Custom Interceptor
xml
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<!-- Define the custom interceptor -->
<interceptors>
<interceptor name="executionTime" class="com.example.interceptor.ExecutionTimeInterceptor"/>
</interceptors>
<!-- Apply the custom interceptor to an action -->
<action name="hello" class="com.example.action.HelloAction">
<interceptor-ref name="executionTime"/>
<interceptor-ref name="defaultStack"/>
<result name="success">/hello.jsp</result>
</action>
</package>
</struts>
Step 3: Create Struts2 Action
java
package com.example.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloAction extends ActionSupport {
private String message;
@Override
public String execute() {
message = "Hello, World!";
return SUCCESS;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
Step 4: Create JSP Page
jsp
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<title>Hello</title>
</head>
<body>
<h1><s:property value="message" /></h1>
</body>
</html>
Explanation of the Example
- 1. Â Custom Interceptor : ExecutionTimeInterceptor.java logs the execution time of an action. It extends AbstractInterceptor and overrides the intercept method.
- 2. Â Interceptor Configuration : The custom interceptor is defined in struts.xml with the name executionTime and is mapped to the hello action.
- 3. Â Struts2 Action : HelloAction.java is a simple action that sets a message.
- 4. Â JSP Page : hello.jsp displays the message set by HelloAction.
This example demonstrates how to create and configure a custom interceptor in Struts2 to log the execution time of actions.