Struts 2 Interceptors with Example

Interceptors :

Interceptor is an object which intercepts an action dynamically. It executed before and after the action execution. It Allows the developers to write a code which can execute and after the action. It executed before and after the code. It can prevent an action before executing.
Features like double-submit guards, type conversion, object population, validation, file upload, page preparation, and more, are all implemented with the help of Interceptors. Each and every Interceptor is pluggable, so you can decide exactly which features an Action needs to support.

As we have seen request workflow in Introduction to struts2 part.

  1. Request is generated by user and sent to Servlet container.
  2. Servlet container invokes FilterDispatcher filter which in turn determines appropriate action.
  3. One by one Intercetors are applied before calling the Action. Interceptors performs tasks such as Logging, Validation, File Upload, Double-submit guard etc.
  4. Action is executed and the Result is generated by Action.
  5. The output of Action is rendered in the view (JSP, Velocity, etc) and the result is returned to the user.

Struts2 comes with default list of Interceptors already configured in the application in struts-default.xml file. We can create our own custom Interceptors and plugin into a Struts2 based web application.Now Internal workflow is:

  • Framework creates an object of ActionInvocation that encapsulates the action and all the interceptors configured for that action.
  • Each interceptors are called before the action gets called.
  • Once the action is called and result is generated, each interceptors are again called in reverse order to perform post processing work.
  • Interceptors can alter the workflow of action. It may prevent the execution of action.

Goal :

We will create two interceptor class and one action class to show workflow . We will  also learn how to declare intercpetor and also to do mapping between interceptor and action in struts.xml.

Create project named “InterceptorsWorkflow”.For configuring struts 2 in your eclipse ide please refer configuring struts 2 link.

Project structure:

Interceptor classes:

Create class in src folder.

copy following code in

Create one more class in src folder. copy following code into

Now both classes implements Interceptor interface. The Interface Interceptor (com.opensymphony.xwork2.interceptor ) extends Serializable interface and is a stateless class. The Interceptor Interface have three methods are as follows-
void destroy( ) – This method is used to clean up resources allocated by the interceptor.
void init( ) – This method is called at the time of intercept creation but before the request processing using intercept, and to initialize any resource needed by the Interceptor.
String intercept(ActionInvocation invocation ) – This method allows the Interceptor to intercept processing and to do some processing before and/or after the processing ActionInvocation.

We will see more about this later.


We have seen in previous tutorial that struts.xml defines mapping between request to corresponding action.
How to declare interceptor in Struts.xml:
In tag in struts.xml, we can declare all our interceptors in tag.
For attaching interceptors to action,we can use under tag
so now we will declare two above interceptors in struts.xml


We will create one action class named under src.Action class will be same as created in previous tutorials.


Create one jsp named “ForCallingAction.jsp” in Webcontent.We are creating this jsp just to call action.We will create a button.On clicking of that button an action “Dummy” will be called.

Create another jsp named “Welcome.jsp” As In DummyAction class,execute returns “success” so we will be directed to this page.


Web.xml will be same as previous tutorials except that we will change welcomeFile to “ForCallingAction.jsp”
Copy following code into web.xml

Run project:

right click on project->run as->run on server
copy resultant url to browser.We will get following page.
 On clicking on button “For calling Dummy Action”  We will get following page
 We got welcome page here but we are more interested in how interceptors worked.So when we will see our console we will get something like this

Request Workflow:

 ActionInvocation object is main part of interceptor.The information of the sequence in which these interceptors are executed for the action is stored in the ActionInvocation object in form of stack.

Action method will only called once the interceptor stack has been called fully.This means that once the first interceptors has been called successfully in the stack it will call next interceptor defined in the stack and there reference being stored in the stack this chain will keep on calling till last interceptor in the stack is called.


This call is key to call next interceptor  defined in the stack or of this is the last it will call the desired function in action class.

Now in other case suppose some of the interceptor failed say workflow it will return the result as INPUT and will halt the further execution of the interceptor and framework will output corresponding error JSP/template to user.

Then come the post processing in this case interceptors will be called in reverse order i.e. top most or latest executed interceptor will be called first and then so on so.

The idea for the post processing is to do any clean-up work or any other things which needs to be done(like cleaning up resources etc)

For the first interceptor,the invoke() method is called by the ActionProxy object.
so in our example

  • FirstInterceptor is called by ActionProxy object and intercept method is called and “start interceptor 1 ” will be printed to console.
  • Now it calls next interceptor in the stack i.e. SecondInterceptor and intercept method is called and “start interceptor 2” will be printed to console.
  • As there are no more interceptors in the stack,so DummyAction’s execute method is called and “In Action” will be printed to console.
  • Now as last interceptor i.e. SecondInterceptor will be called first so “end interceptor 2” will be printed to console.
  • Now first interceptor i.e. FirstInterceptor will be called and “end interceptor 1” will be printed to console.

Source code:

Now in next post,we will learn how to upload file on server in struts 2

Related Posts

  • nvarchar vs varchar
    29 July

    nvarchar vs varchar in SQL Server

    Table of ContentsIntroductionVARCHARNVARCHARExamples to Demonstrate Difference between VARCHAR and NVARCHARVARCHAR vs NVARCHAR Comparison Chart This article will look at the difference between the data types NVARCHAR and VARCHAR in SQL Server. These data types are commonly used for storing textual data or strings for various design purposes. We will look at the description of each […]

  • Stream2watch
    29 July

    Stream2watch alternatives -13 Best Websites Like Streamtowatch in 2021

    Table of ContentsBest Working Proxies of the Stream2watch in 2021Stream2Watch alternativesSport365SportP2PCricFree12th PlayerVIP BoxLiveTVWiziwigStopStreamSport LemonMamaHDLAOLA1Sport RARTime4TVfuboTVFirstRowSportsVIP LeagueFrequently Asked Questions**Is Stream2watch a safe website?What are the best Streamtowatch Alternatives?Do you need an account for Stream2watch?Final Words Are you after the best Stream2watch alternatives? Good News! this article is all about efficient web-based resources for streaming football, baseball, […]

  • 29 July

    AirPod Case not Charging – Common Issues and Fixes

    Table of ContentsBefore you StartWhy Is My AirPod Case Not Charging?Five Easy Solutions for AirPods Case Not Charging1. Reset Your AirPods Case2. Clean the Charging Port 3. Get a new Charger and Cable4. Update the AirPods Firmware5. Replace the CaseTips for Avoid AirPod not Charging IssueFrequently Asked QuestionsWhy isn’t my AirPod case charging, but my AirPods […]

  • Mobdro alternatives
    29 July

    Mobdro Alternatives – 14 Best apps like Mobdro in 2021

    Table of ContentsBest Mobdro Alternatives Working in 2021Cyberflix TVMorph TVTerrarium TVTea TVVisit Thop TVTubi Tv123MoviesMovie BoxVuduOreo TVMegaBox HDFreeFlixYuppTVFrequently Asked QuestionsWhat is Mobdro?Why do you need Mobdro alternatives?Are there free Mobdro alternatives?Wrapping Up Can’t access Mobdro anymore and are worried about watching your favorite movies? Take a breath; you still have a long list of working […]

  • Select rows by multiple conditions using loc in Pandas
    29 July

    Select rows by multiple conditions using loc in Pandas

    The loc() function in a pandas module is used to access values from a DataFrame based on some labels. It returns the rows and columns which match the labels. We can use this function to extract rows from a DataFrame based on some conditions also. First, let us understand what happens when we provide a […]

  • Split dataframe in Pandas
    28 July

    Split dataframe in Pandas

    Table of ContentsUsing the iloc() function to split DataFrame in PythonBy RowsBy ColumnsUsing the sample() function to split DataFrame in PythonUsing the groupby() function to split DataFrame in PythonUsing the columns to split DataFrame in Python In real-life scenarios, we deal with massive datasets with many rows and columns. At times, we may want to […]


  1. Hi,

    Will the interceptor intercepts the request before the jsp file configured in welcome file-list gets displayed. please clarify


  2. Hi,
    Thanks for your post.
    But there are 2 minor errors in the part talking about struts.xml.
    There is no LoginAction properties.
    There is no Login.jsp

  3. Because dis is just a simple message displaying demo not a login demo.. So we do not need login action properties nor login.jsp

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.