Struts2 Custom Interceptor Example, Struts2 Interceptors

Struts » on Apr 4, 2012 { 5 Comments } By Sivateja

Let us see how to create user defined interceptors in struts2, we already know this fact that struts2 by default provided lot of interceptors. In fact we no need to create any custom interceptors 🙂 , but this is the way to create custom interceptors if you would like to.

Let us see the steps to create our own interceptor

  • Create one java class which should implements from Interceptor interface, see all interceptors in struts2 must implements from Interceptor interface only
  • Override all abstract methods in Interface interface, Interceptor contains 3 abstract methods init(), destroy(), intercept(ActionInvocation inv)
  • Finally configure our interceptor class in struts.xml file that’s it, let us see the example

Struts2 Custom Interceptor Example

Required files…

  • index.jsp
  • success.jsp
  • web.xml
  • struts.xml

Directory Structure


<META HTTP-EQUIV="Refresh" CONTENT="0;URL=verify.action">


<%@ taglib prefix="s" uri="/struts-tags" %>

Struts 2 Interceptors executed successfully, <br>
you can find the same in console.

package java4s;
import com.opensymphony.xwork2.ActionSupport;

public class LogingEx extends ActionSupport{
    private static final long serialVersionUID = 1L;    

    public String execute()
        System.out.println("In action class execute method...!!");

        } catch(Exception e)


        return SUCCESS;


package java4s;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class MyInterceptor implements Interceptor{
    private static final long serialVersionUID = 1L;    

    public void init()
        System.out.println("Interceptor init method called.....");

    public void destroy()
        System.out.println("Interceptor destroy method called.....");

    public String intercept(ActionInvocation inv) throws Exception
        System.out.println("This is before invocation.invoke()...");

        String a = inv.invoke();

        System.out.println("This is after invocation.invoke()...");
        // here string a contains "success"
        return a;



<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="2.5">


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

    <include file="struts-default.xml"/>
    <package name="a" extends="struts-default">


                 <interceptor name="myOwn" class="java4s.MyInterceptor"></interceptor>

                 <interceptor-stack name="dummyName">
                       <interceptor-ref name="myOwn"/>


        <action name="verify" class="java4s.LogingEx">
        <interceptor-ref name="dummyName"/>
        <result name="success">/success.jsp</result>



In, line number 23 we are calling invoke() method of ActionInvocation. Actually invoke() method will take cares the total journey of executing pre-post logic for our Action class.



You Might Also Like

  ::. About the Author .::

Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.


5 Responses to “Struts2 Custom Interceptor Example, Struts2 Interceptors”
  1. Mohammed Vaseem says:

    Hello java4s,

    Please provide the concepts of ValueStack, ActionContext, OGNL as these are the fundamentals to struts2.

    Mohammed Vaseem

  2. Sri says:

    Hi java4s

    Please provide more Real time points

  3. Pooja Tekwani says:

    Hello Sir,

    I am using Struts2 Jquery Plugin, and if I click on two radio buttons alternatively than two different should be loaded… If you can guide me with my query than it’ll be really helpful to me…

    Thanks & Regards,
    Pooja Tekwani

  4. Hello, Sivateja

    I am continue reader of you post. I read all your post and everything is helpful for me and everyone . I need one example of Struts 2x with display taglib library for pagination. I used Struts 1x and it’s working fine .
    so please provide me any small example as soon as possible which will be helpful for me and another one also.

    Narottam Singh

  5. Yogesh K says:


    I am working on One application and I am calling json action for login functionality. I want to use interceptor to deny multilogin for same user.

    I am trying to add customer inteceptor in Json-default package, but it will throw exception.
    Kindly help me to resolve the same.



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.