life cycle of an interceptor
In Struts2, interceptors are used to intercept requests before they reach the action and after the action execution. The lifecycle of an interceptor involves several steps, from initialization to destruction. Understanding the lifecycle is crucial for effectively implementing and managing interceptors.

Table of Contents
Lifecycle Stages:
- 1. Â Initialization (init Method) : When the Struts2 framework starts, it initializes all configured interceptors by calling their init method. This is where any one-time setup logic should be placed.
- 2. Â Request Interception (intercept Method) : Each time a request is made, the intercept method is called. This method contains the core logic of the interceptor, which can include pre-processing (before the action is executed) and post-processing (after the action is executed).
- 3. Â Cleanup (destroy Method) : When the Struts2 framework shuts down, it calls the destroy method of each interceptor. This is where any cleanup logic should be placed, such as releasing resources.
Key Points:
- The init method is called only once when the interceptor is first created.
- The intercept method is called for every request that the interceptor intercepts.
- The destroy method is called only once when the interceptor is destroyed.
Java Example
Let’s create an interceptor that logs messages at each stage of its lifecycle.
Step 1: Create the Custom Interceptor
java
package com.example.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class LifecycleInterceptor implements Interceptor {
@Override
public void destroy() {
System.out.println("LifecycleInterceptor: destroy method called");
}
@Override
public void init() {
System.out.println("LifecycleInterceptor: init method called");
}
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("LifecycleInterceptor: before action execution");
String result = invocation.invoke();
System.out.println("LifecycleInterceptor: after action execution");
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="lifecycle" class="com.example.interceptor.LifecycleInterceptor"/>
</interceptors>
<!-- Apply the custom interceptor to an action -->
<action name="hello" class="com.example.action.HelloAction">
<interceptor-ref name="lifecycle"/>
<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 : LifecycleInterceptor.java implements the Interceptor interface and provides implementations for the init, intercept, and destroy methods. It logs messages at each stage of the lifecycle.
- 2. Â Interceptor Configuration : The custom interceptor is defined in struts.xml with the name lifecycle 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 a custom interceptor in Struts2 and log messages at different stages of its lifecycle.