Custom Interceptor in Struts 2

What are Interceptors?
Interceptors are used for performing operations like Validation, file upload, exception handling etc.

Advantages of Interceptors:
Separation of Concerns, If we need to remove any of the concerns like validation or exception handling etc.. We can remove them from struts.xml without disturbing the application.

Struts 2 provide a set of default interceptors (default interceptors), we can also create and implement our own interceptors. Creating and implementing own interceptors are known as Custom Interceptors.

Lets see an example for custom interceptors, We are going to check the time for the process.

Structure:
struct

Lets create 2 JSP files – input.jsp and showdetails.jsp

input.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Custom Interceptor</title>
    </head>
    <body>
        <s:form action="login">
            <s:textfield name="name" label="Name"/>
            <s:submit value="Intercept"/>
        </s:form>
    </body>
</html>

showdetails.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Intercepted</title>
    </head>
    <body>
        <h1>Intercepted Successfully, <s:property value="name"/> </h1>
    </body>
</html>

login.java

package pojo;

public class login {
    String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public String execute()
    {
        name=name.toUpperCase(); //lets change the name to uppercase
        return "success";
    }
}

MyInterceptor.java

package pojo;

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

public class MyInterceptor implements Interceptor {

    @Override
    public void destroy() {
        
    }

    @Override
    public void init() {
      
    }

    @Override
    public String intercept(ActionInvocation ai) throws Exception {
        String classname = ai.getAction().getClass().getName();
        long startTime = System.currentTimeMillis();
        System.out.println("Start time is: "+startTime+" ms");
        String result = ai.invoke();
        return result;

    }
    
}

struts.xml

<struts>
    <include file="example.xml"/>
    <!-- Configuration for the default package. -->
    <package name="default" extends="struts-default">
        <interceptors>
            <interceptor name="timecheck" class="pojo.MyInterceptor"></interceptor>
            <interceptor-stack name="time">
                    <interceptor-ref name="timecheck" />
                    <interceptor-ref name="defaultStack" />
            </interceptor-stack>
        </interceptors>
        
        <action name="login" class="pojo.login">  
            <interceptor-ref name="time"></interceptor-ref>  
        <result name="success">showdetails.jsp</result>  
        </action>  
    </package>
</struts>

Output:
input
show
Console output to check the timing function,

[2015-09-18T20:52:01.795+0530] [glassfish 4.0] [INFO] [] [] [tid: _ThreadID=101 _ThreadName=Thread-3] [timeMillis: 1442589721795] [levelValue: 800] [[
Start time is: 1442589721795 ms]]

 

 

 

 

 

By Sri

Leave a Reply

Your email address will not be published. Required fields are marked *