Struts 2 custom interceptor

We can create a custom interceptor in struts 2. To create a custom interceptor implements the Interceptor interface. Interceptor interface provides the methods to create a custom interceptor.

Methods of Interceptor interface:

1. init(): It is used to initialize the interceptor. It is called only once by the web container.

public void init()

2. intercept(ActionInvocation invocation): It is used to define the processing logic of the interceptor.

Syntax: public void intercept(ActionInvocation invocation) throws Exception

3. destroy(): It is used for clean-up process before destroying the interceptor.

public void destroy()

Struts 2 custom interceptor example:

login.jsp

<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
	<head>
		<title>Struts 2 custom interceptor example</title>
	</head>
	<body>
		<h3>This is a custom interceptor example.</h3>
 
		<s:form action="Login">
		 <s:textfield name="userName" label="UserName" />
		 <s:submit value="Hello" align="center"/>
		</s:form>
 
	</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
 
 	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>
        	org.apache.struts2.dispatcher.ng.
        	filter.StrutsPrepareAndExecuteFilter
        </filter-class>
	</filter>
 
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
 
	<welcome-file-list>
	  <welcome-file>login.jsp</welcome-file>
	</welcome-file-list>
 
</web-app>

struts.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="user" extends="struts-default">
 
		<interceptors>  
			<interceptor name="welcomeIntercepter" 
			  class="com.w3schools.
			   customintercepter.CustomIntercepter">
			</interceptor>  
		</interceptors> 
 
		<action name="Login" 
		          class="com.w3schools.action.Login">
		  <interceptor-ref name="defaultStack"/> 
		  <interceptor-ref name="welcomeIntercepter"/>  
		  <result name="success">/welcome.jsp</result>
		</action>
	</package>
 
</struts>

Login.java

/**
 * This class is used as an action class.
 * @author w3schools
 */
public class Login {
	//data members
	private String userName;
 
	//business logic
	public String execute(){
		System.out.println("Login's execute() " +
				"is called...");
		return "success";	
	}	
 
	//getter setters
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
}

CustomIntercepter.java

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.opensymphony.xwork2.util.ValueStack;
 
/**
 * This class is used for custom intercepter.
 * @author w3schools
 */
public class CustomIntercepter  implements Interceptor{
 
	public void init() {
			System.out.println("CustomIntercepter's " +
					"init methods is called.");
	}
 
	public String intercept(ActionInvocation 
			actionInvocation) throws Exception {
		System.out.println("Before " +
				"actionInvocation.invoke() called.");
		 ValueStack stack = actionInvocation.getStack();  
	     String userName = stack.findString("userName"); 	          
	     stack.set("userName","Hi " + userName + 
	    		 ", welcome."); 	          
	     String resultString = actionInvocation.invoke();  	
	     System.out.println("After " +
	     		"actionInvocation.invoke() called.");
	     return resultString;
	}
 
	public void destroy() {
		System.out.println("CustomIntercepter's " +
				"destroy methods is called.");
	}
 
}

welcome.jsp

<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
	<head>
	  <title>Struts 2 custom interceptor example</title>
	</head>
	<body>
	  <h3>This is a custom interceptor example.</h3>
	  <h4><s:property value="userName" /></h4>	 
	</body>
</html>

Output:

struts 10 login
 
Enter UserName.
struts 10 value
 
Click on Hello button.
struts 10 final

On Console:

CustomIntercepter's init methods is called.
Before actionInvocation.invoke() called.
Login's execute() is called...
After actionInvocation.invoke() called.

 
Download this example.
 
Next Topic: Struts 2 result type with example.
Previous Topic: Struts 2 execAndWait interceptor with example.