You can view the tutorials best in Google Chrome, Mozilla Firefox, Opera, higher version of Internet Explorer

Working With Struts 2 Interceptor With Example

Struts » On Oct 29, 2011 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

success.jsp

LogingEx.java

web.xml

struts.xml

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







By ( Java/J2EE Developer & Web Designer )
Founder of Java4s.com. You can contact me at sivateja [at] java4s.com


Comments

6 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!

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
So, What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs

Most Recent Tutorials
Hibernate Recent Posts
Spring Recent Posts
Struts Recent Posts
Recomandded Links
Current & UpComing Tutorials
Sitemap :
spring Hibernate struts Json Ajax Log4j Log4j coreJava Servlets Web Services


Upcoming
  Web Services [ SOAP ], JSP
Facebook_Java4s  Twitter_Java4s  GooglePlus_Java4s  Youtube_Java4s 
© 2010 - 2014 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.