Struts2.2.1 Interceptor Example
Posted on: January 29, 2011 at 12:00 AM
 In this tutorial, We will discuss about Interceptor example and how to implements Interceptor In the Struts 2.2.1 applications.

Struts2.2.1  Interceptor Example

 In this tutorial, We will discuss about Interceptor example and how to implements Interceptor In the Struts 2.2.1 applications. Here, we use a struts configuration file 'struts.xml' for Interceptor Configuration of the application with the Interceptor and Interceptor-ref tags. 

The following Example will shows how to implement the Interceptor in the Struts2.2.1 --

First we create a JSP file named index.jsp as follows.ADS_TO_REPLACE_1

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"ADS_TO_REPLACE_2

"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>ADS_TO_REPLACE_3

<title>Interceptor Example</title>

</head>

<body>ADS_TO_REPLACE_4

<h1>Interceptor Example</h1>

<hr>

<a href="Interceptor.action">Interceptor Example</a>ADS_TO_REPLACE_5

</body>

</html>

Here is the Interceptor.jsp 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"ADS_TO_REPLACE_6

pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">ADS_TO_REPLACE_7

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">ADS_TO_REPLACE_8

<title>Interceptors demo example</title>

</head>

<body>ADS_TO_REPLACE_9

Your welcome, The Process is successful.

</body>

</html>

Here is the  struts.xml  - The Interceptor Configuration is takes place in this file ADS_TO_REPLACE_10

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">ADS_TO_REPLACE_11

<struts>

<package name="roseindia" extends="struts-default">

 <interceptors>ADS_TO_REPLACE_12

                    <interceptor name="logger" class="roseindia.InterceptorAction">

                     </interceptor>

                 <interceptor-stack name="loggerStack"> ADS_TO_REPLACE_13

                               <interceptor-ref name="logger" />

                              <interceptor-ref name="defaultStack" />

                 </interceptor-stack>ADS_TO_REPLACE_14

</interceptors>

<action name="Interceptor" class="roseindia.InterceptorAction">

                <interceptor-ref name="loggerStack" />ADS_TO_REPLACE_15

                <result name="success">/Interceptor.jsp</result>

               <result name="error">/index.jsp</result>

</action>ADS_TO_REPLACE_16

</package>

</struts>

The action class InterceptorAction.java is as follows.

The Interceptor class must Implements com.opensymphony.xwork.interceptor.Interceptor. This class have three methods.

 init()- 
This method is called when interceptor initialize,
destroy()- 
This method is called when Interceptor destruct,
intercept(ActionInvocation invocation)-
This method is called where we place the code to do work.ADS_TO_REPLACE_17

package roseindia;

import com.opensymphony.xwork2.ActionInvocation;

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

public class InterceptorAction implements Interceptor {

public String intercept(ActionInvocation invocation) throws Exception {

String className = invocation.getAction().getClass().getName();ADS_TO_REPLACE_19

long startingTime = System.currentTimeMillis();

System.out.println("Before calling action: " + className);

String result = invocation.invoke();ADS_TO_REPLACE_20

long endingTime = System.currentTimeMillis();

System.out.println("After calling action: " + className

+ " Time taken: " + (endingTime - startingTime) + " ms");ADS_TO_REPLACE_21

return result;

}

public void destroy() {ADS_TO_REPLACE_22

System.out.println("Destroying InterceptorAction...");

}

public void init() {ADS_TO_REPLACE_23

System.out.println("Initializing InterceptorAction...");

}

public String execute() {ADS_TO_REPLACE_24

return "success";

}

}

This Program produces output on the basis of the Interceptor Action, This  give the output as-ADS_TO_REPLACE_25

Output:-

The Interceptor Action on the console during the begining to ending of the session  is shown as. the given time is the time consumed by the interceptor when Action (Interceptor.jsp) is run.

Initializing InterceptorAction...
....
....
....
Before calling action: roseindia.InterceptorAction

....
....
....
After calling action: roseindia.InterceptorAction Time taken: 74 ms

...
...
....
Destroying InterceptorAction...
ADS_TO_REPLACE_26
 

index.gif

 

Interceptor.gif

ADS_TO_REPLACE_27

Download Select Source Code

Related Tags for Struts2.2.1 Interceptor Example:

Advertisements

Ads

Ads

 
Advertisement null

Ads