Newsletter

Working With Struts 2 Interceptor With Example

Struts » on Oct 29, 2011 { 9 Comments } By Sivateja

In struts 1.x we have a RequestProcessor class if we want any per-processing services and post processing services for an Action, so that we used to implement in the RequestProcessor class, but this services will be executed for all the Actions classes in our application, this is the main disadvantage.

So to avoid this we have Interceptors in struts 2.x, with these interceptors we can get post, pre processing services for particular Action class, rather for getting to all Actions classes.

An interceptor will intercept a client request, and execute some per-processing and post-processing services for an Action class business logic.  The main advantages of this interceptors are, we can apply different-different pre-post services to the different-different actions.  Means no need to apply same services to all Actions.

An interceptor in struts 2.x is equal to RequestProcessor in struts 1.x, but we have a single RequestProcessor for a module, where as we have many interceptors in 2.x

All interceptors are classes, which implements interceptor interface, so we must override all methods in the interceptor interface.  So instead of implementing am going to extend our class from AbstractInterceptor.

In the interceptor interface we have 3 methods

  • init
  • intercept
  • destroy

Actually no need to create any interceptors manually, as we already have lot of predefined interceptors.  By default to all the Action classes 8 interceptors are attached, means for every Action class before reaching execute() method 8 set of interceptors will be executed.  This set is called Default Stack.  All the interceptors are configured in struts-default.xml

But in case if we want to create our own interceptor, then no need to configure our interceptor in struts-default.xml, we can directly configure in struts.xml it self.

Interceptor interface location is com.opensymphony.xwork2.interceptor.*

And all this journey,  executing interceptors, then business logic in execute() method,  i mean all controller moments will be taking care by ActionInvocation only

 Pre, Post processing logic in interceptors are separated by calling invoke() method

files required

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

Directory Structure

index.jsp

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

success.jsp

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

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

LogingEx.java

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

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

    public String execute()
    {
        try
        {
            Thread.sleep(4000);
        } catch(Exception e)
        {

        }        

        return SUCCESS;
    }

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <include file="struts-default.xml"/>
    <package name="a" extends="struts-default">
        <action name="verify" class="java4s.LogingEx">

        <interceptor-ref name="defaultStack" />
        <interceptor-ref name="timer"/>

        <!--
        <interceptor-ref name="execAndWait">
          <param name="dalay">2000</param>
        </interceptor-ref>
         -->
            <result name="success">/success.jsp</result>
        </action>
    </package>
</struts>

Output

Output1

Notes:

  • Remember these interceptors will shows the result in the console automatically, we need to write any System.out.println(), check above console showing that our timer took 4719 ms 🙂 .
  • You may get one doubt, we have given Thread.sleep(4000); in LogingEx.java then it has to show 4000 ms instead 4719 ms
  • The reason being we have executing defaultstack also ( struts.xml — line number 11 ) so this extra 719 is because of defaultstack

​​

You Might Also Like

  ::. About the Author .::

Java4s_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.

Comments

9 Responses to “Working With Struts 2 Interceptor With Example”
  1. Mohammed Vaseem says:

    Please provide some more examples on interceptors for still better understanding..
    Thanks.

  2. Java4s says:

    @Vaseem

    Still you need ? 🙂

    Hmm.. let me give an example on, how to create user-defined interceptor, then you will be able to understand what is what i guess.

  3. Mohammed Vaseem says:

    As interceptors is the key concept, “better in sense” to provide more concepts of interceptors like list of interceptors, where and when which interceptor is used etc. I hope you are getting me..

  4. Java4s says:

    @Vaseem

    Got you, will take of this.
    Thanks for our valuable suggestions always.

  5. narayana says:

    HI,

    you noticed that pre-processing services and post processing services in tnterceptors. give me examples for pre and post services.

  6. ch.kmvprasad says:

    wen we use custom interceptor, and our application contains what ever input values,like uname ,city etc.i cant able to get that input values into action class !. i get values as null!.
    except that every thing perfect!
    pls provide a solution for me!
    ThQ very Much!

  7. Amarnath says:

    how to stop interceptor calls for particular request.

  8. Trisaiteja says:

    Thanks for this great tutorial really it is so helpful to the starters.

    Can you please provide more examples on this interceptors concept???

  9. nupur says:

    Can you provide me more clearfication as I am not able to understand what is the use of Interseptor

Name*
Mail*
Website



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.