Struts - SPLessons

Chapter 16

Struts 2 Interceptors

Struts 2 Interceptors

Struts 2 Interceptors is an object, which is invoked (executed) the pre-processing request and post-processing request. Interceptors are used to perform the validations, exceptions handling and display the result in between the execute() method. Struts 2 Interceptors execution is same as the servlet filter execution type.The advantage of Interceptors is, One can run the application using validations, after sometimes remove the validations on that application, and one cannot deploy the application in the server again. So, directly start the server application and execute. Following are the default interceptors of Struts 2.

Interceptors are executed, whenever the request is coming to the container at that time container first will check to use any interceptors to be executed and execute the Action class execute(). This process is executed by using invoke().invoke() contain ActionInvocation interface object.

String interceptor =ActionInvocation.invoke(); 

If one have to write the custom Interceptors class, that class will be extended to AbstractInterceptor class

By using interceptors in the application, one can use the tag in struts.xml file.

interceptor class reference id

For example, let’s take one interceptor class and execute.

Create the project directory structure.

Add the jar files in lib folder. Struts2 jar files and servlet-api.jar.

Create the View pages and forwarded the request to Action class.

<%@ taglib uri="/struts-tags" prefix="s" %>
  <s:form action="employee"> 
      <s:textfield name="id" label="Employee Id">
      <s:submit value="submit">

The s:textfield is used to create a HTML input textbox.

To map the URL pattern in a web.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns:web="" xsi:schemaLocation="http://xml">

Here make sure that filte-name should be same.

Create the interceptor class and Action class.

package com.itoolsinfo;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class EmployeeIntercepter extends AbstractInterceptor
  public String intercept(ActionInvocation invocation)throws Exception

   /* let us do some pre-processing */
   String output = "Pre-Processing interceptor"; 

   /* let us call action or next interceptor */
   String result = invocation.invoke();

  /* let us do some post-processing */
   output = "Post-Processing interceptor"; 

    return result;

Here used invocation.invoke() method to invoke action.

package com.itoolsinfo;

public class EmployeeAction 
 private int id;

 public int getId()
 return id;

 public void setId(int id) 
 { = id;
 public String execute()
 System.out.println("It is the main class execute method");
 return "success";

Here used Set and Get methods to perform on id.

Configure the interceptor class and Action class in struts.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "">

    <constant name="struts.devMode" value="true" />
  <package name="default" extends="struts-default">

      <interceptor name="myinterceptor" class="com.iquickinfo.EmployeeInterceptor"/>
    <action name="employee" class="com.itoolsinfo.EmployeeAction" method="execute">
        <interceptor-ref name="params"/>
        <interceptor-ref name="myinterceptor" />
        <result name="success">

Where success is a predefined result type. action element is the sub component of package. It speaks to an activity to be conjured for the approaching request. It has name, class and method attributes. In the event that you don’t determine name property as a matter of course execute() technique will be summoned for the predetermined actiion class.

Forward the response into the browser.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<%@ taglib prefix="s" uri="/struts-tags" %>

    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
     Itoolsinfo Employee Details successfully saved.

     Employee Id :<s:property value="id">

The property tag is used to get the property value from a class, which will default to the current Action class property if none is specified.

Run the application in server, the output is displayed as below.
Enter the employee id and click on submit button, then the output is displayed on console.
Display the output in browser.


  • Struts 2 Interceptors are pluggable
  • Struts 2 Interceptors are same like Servlet fileters.
  • Internalization task will be performed by interceptors.