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

  • 22 July

    Manage C Sharp Homework Assignment at Ease Online

    Table of ContentsC Sharp Homework Help: Use the Service and Get Your BenefitsC Sharp Assignment Help: Sound Guarantees for the CustomersTop-Notch Authentic ContentPrivacy24/7 SupportPlace Your Order and Get the Task Down at Ease In this post, we will see how to manage C sharp homework assignment online. Who can do my c# assignment fast? The […]

  • Break out of nested loop in java
    22 July

    How to break out of nested loops in Java

    Table of ContentsUsing break (will break inner loop)Using named loopUsing named blockUsing returnConclusion In this post, we will see how to break out of nested loops in Java. Using break (will break inner loop) It is very important to understand how nested loops work to ensure that applying break will output the desired result. If […]

  • showbox alternatives
    22 July

    15 Best Showbox Alternatives (With Download Links) in 2021

    Table of ContentsBest Showbox Alternatives to Try in 2021CrackleMovie HDPlaybox HDMovie BoxMegaBox HDTubi TvHuluPopcorn TimeCinema BoxFreeFlixMobdroVisit Popcorn FlixBobby Movie BoxVisit Flipps HDMovcyFrequently Asked QuestionWhat is Showbox?What is the need for Showbox alternatives?Which are the best Showbox alternatives?Are there free Showbox alternatives?Final Words Were you having trouble streaming your favourite movies, which made you look for […]

  • Kimcartoon alternatives
    21 July

    KimCartoon alternatives -13 Best Websites Like KimCartoon in 2021

    Table of ContentsBest KimCartoon alternatives in 2021CartoonsOnToonovaKissAnimeToonGetWatch Cartoons OnlineCartoonCrazyAnimedaoAnimeToonCartoon ExtraEyeonanimeNyaaMasteraniToonJetFrequently Asked Questions Is KimCartoon safe in 2021?Is KimCartoon legal in 2021?Is there any working proxy of KimCartoon in 2021?Wrapping Up Are you worried about KimCartoon getting shut recently? Well, we’ve you covered. KimCartoon has been one of the best platforms for seamlessly streaming cartoons in HD. […]

  • Trim String in C++
    19 July

    Trim String in C++

    Table of ContentsUsing Boost string algorithms to trim string in C++Using find_first_not_of() and find_last_not_of()` to trim strings in C++Using find_if() to trim the string in C++Using stringstream to trim the strings in C++Using a customized function to trim the strings in C++Conclusion When we take an input from an user, strings can have unwanted whitespaces […]

  • JSON parser in C++
    19 July

    JSON Parser in C++

    Table of ContentsSimple JSON Parser in C++ using JsonCpp libraryConclusion In this post, we will see about JSON parser in C++. There is no native support for JSON in C++. We can use a number of libraries that provide support for JSON in C++. We will use JsonCpp to parse JSON files in C++ which […]


  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.