• October 3, 2014
  • Struts2
  • Comments Off on Struts 2 custom interceptor

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.

Syntax: 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.

Syntax: public void destroy()

Struts 2 custom interceptor example:


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


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 


"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
	<package name="user" extends="struts-default">
			<interceptor name="welcomeIntercepter" 
		<action name="Login" 
		  <interceptor-ref name="defaultStack"/> 
		  <interceptor-ref name="welcomeIntercepter"/>  
		  <result name="success">/welcome.jsp</result>


 * This class is used as an action class.
 * @author javawithease
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;


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 javawithease
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.");


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


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.

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.


Comments are closed.