interceptor in Struts2
In the Struts2 framework, interceptors are a key component that provide a way to preprocess and post-process requests. They act as a powerful mechanism to apply cross-cutting concerns such as logging, validation, exception handling, and more across multiple action classes. Interceptors are defined in the struts.xml configuration file and can be applied globally or to specific actions.

Table of Contents
Key Features
- 1. Â Cross-Cutting Concerns : Interceptors help in handling concerns that span across multiple layers, such as logging, validation, and authentication.
- 2. Â Reusability : Interceptors can be reused across different actions, reducing redundancy in the codebase.
- 3. Â Flexible Configuration : They can be configured globally for all actions or specifically for individual actions.
- 4. Â Stackable : Multiple interceptors can be applied in a stack, allowing for a sequence of preprocessing and post-processing tasks.
How Interceptors Work
When a request is made, it passes through a stack of interceptors before reaching the action. Each interceptor can perform its task before and/or after the action is executed. This provides a clean way to manage preprocessing and post-processing logic.
java
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class LoggingInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
// Pre-processing logic
System.out.println("Before action execution: " + invocation.getAction().getClass().getName());
// Proceed to the next interceptor or action
String result = invocation.invoke();
// Post-processing logic
System.out.println("After action execution: " + invocation.getAction().getClass().getName());
return result;
}
}
xml
<struts>
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="logging" class="com.example.interceptor.LoggingInterceptor"/>
<interceptor-stack name="customStack">
<interceptor-ref name="logging"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<action name="helloWorld" class="com.example.action.HelloWorldAction">
<interceptor-ref name="customStack"/>
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
java
package com.example.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport {
private String message;
public String execute() {
message = "Hello, World!";
return SUCCESS;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
Explanation
- 1. Â Custom Interceptor : The LoggingInterceptor class extends AbstractInterceptor and overrides the intercept method to include pre-processing and post-processing logic. This interceptor logs messages before and after the action execution.
- 2. Â Configuration : The struts.xml file configures the LoggingInterceptor and adds it to a custom interceptor stack named customStack. This stack is then applied to the helloWorld action.
- 3. Â Action Class : The HelloWorldAction class is a simple action that sets a message and returns a success result. The interceptor stack applied to this action ensures that logging occurs before and after its execution.
Conclusion
Interceptors in Struts2 provide a powerful mechanism for managing cross-cutting concerns and preprocessing and post-processing logic in a clean and reusable manner. By defining custom interceptors and configuring them in the struts.xml file, developers can streamline their action processing and maintain cleaner codebases.