16_7 - ExecAndWait Interceptor

Introduction:

  • The chapter will explain the interceptor “ExecAndWait” in Struts 2 with an example program.

Execute and Wait Interceptor:

  • As the name suggests, execute and wait interceptor is used to wait before the execution of an application.
  • The application is developed in such a way that waiting page is displayed before the next page is executed. So the sequence of execution is:

First.jsp -> wait.jsp -> next.jsp (or other JSP page)

  • In our application, we are developing a login form for employees. The first page will have form elements for inputting of user credentials, and will display next page only if the credentials are verified. Now during the process of authentication (might take a little longer time), we can display a custom waiting page to inform user about anything. That’s where the exec and wait interceptor comes in the picture.
  • So the first.jsp can be coded as:
// first.jsp

<%-- 
    Document   : first
    Created on : Nov 20, 2014, 12:34:17 PM
    Author     : Admin
--%>

<%@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> Execute and Wait Demo - Employee Login Application </title>
    </head>
    <body>
        <h1>            
            Employee Login System            
        </h1>

        <hr/>
        <s:form action = "click">
            <s:label value = "Enter username : "/> <s:textfield name = "name"/>
            <br/>
            <s:label value = "Enter password : "/><s:textfield name = "pwd"/>
            <br/>
            <s:submit/>            
        </s:form>
   </body>
</html>
  • Now the next page will simply print users name on successful login.
  • So, next.jsp can be written as follows:
<%-- 
    Document   : next
    Created on : Nov 20, 2014, 12:50:20 PM
    Author     : Admin
--%>

<%@page contentType = "text/html" pageEncoding = "UTF-8"%>
<%@taglib prefix = "s" uri = "/struts-tags" %>
<%@page  language = "java" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
        <title> Employee Page </title>
    </head>
    <body>

      <h1> Login success.. </h1>
      <hr/>  
      <h2>            
            Welcome <s:property value = "name"/>
        </h2>
</body>
</html>
  • The action class will only contain getter and setter methods of all the variables used in our first.jsp.  The additional thing to be taken care of in action class is to provide a timed waiting (for displaying message of waiting page) through a timed loop (any loop – for, while or do while) or simply by making the current thread sleep of some milliseconds of time.
  • In our example, we are making the programs thread sleep for 5 seconds (i.e. 5000 milliseconds) after user’s the credentials are verified.
  • So Myaciton1.java will be coded as:
// Myaction_1.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

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

/**
 *
 * @author Admin
 */
public class Myaction_1 extends ActionSupport
{
   private String name, pwd;
   public void setName(String name)
        {
            this.name = name;
        }
        public void setPwd(String pwd)
        {
            this.pwd = pwd;
        }

        public String getName()
        {
            return name;
        }

        public String getPwd()
        {
            return pwd;
        }

   public String execute() throws Exception
        {
            if(name == “abc” && pwd == “abc”)
            {
                        Thread.sleep(5000);
                         return SUCCESS;                        
            }
            else
            {
                return “failure”;
            }            
       }  
  }
  • Also the waiting page is to be developed additionally so that it can display any useful message to the user.
  • Again, this page must be refreshed so that it does not continue to display the same message again and again. This refreshing of a page can be done by meta tags of HTML.
  • That is,

    <meta http-equiv = "Refresh" content = "number of seconds after which the page should be refreshed"/>

  • The meta tag of HTML will automatically refresh the page as per the seconds specified in the content attribute of the meta tag.
  • So wait.jsp can be coded as:
// wait.jsp

<%-- 
    Document   : wait
    Created on : Dec 3, 2014, 3:30:59 PM
    Author     : Admin
--%>
<%@page contentType = "text/html" pageEncoding = "UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv = "Refresh" content = "3"/>
        <title> Employee Login System </title>
    </head>
    <body>

        <h1>
           Employee Login System
        </h1>
        <hr/>
        <h2>
            Please wait while we verify your credentials.. 
        </h2>

    </body>
</html>
  • Now to combine our JSP’s with action class, we need to configure our struts.xml. 
// struts.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <constant name = "struts.devMode" value = "true"></constant>
     <package name = "default" extends = "struts-default">
        <action name = "click" class = "action_class.Myaction_1">
             <interceptor-ref name = "execAndWait">
                   <param name = "delay"> 5000 </param>
                   <param name = "delaySleepInterval"> 500 </param>
              </interceptor-ref>
              <result name = "wait"> wait.jsp </result>
              <result name = "success"> next.jsp </result>
              <result name = "failure"> first.jsp </result>
         </action>
    </package>
</struts>
  • The execute and wait interceptor is given by following syntax:

        <interceptor-ref name = "execAndWait">

             <param name = "delay"> Number of seconds for initial delay time </param>

             <param name = "delaySleepInterval"> Number of seconds to wait to check the background process </param>

            <param name = "threadPriority”> Integer priority of thread </param>

        </interceptor-ref>

  • The parameters for executing and wait interceptor are:
    • delay – the initial delay time for display of waiting page (wait.jsp in our example)
    • delaySleepInterval – the number of seconds to wait before the next.jsp is to be displayed.
    • threadPriority – integral value of thread priority
  • These parameters are not mandatory to use. In our application as we are having only one process of execution (that is only one execute method), we do not required to configure threadPriority parameter.
  • The web.xml can be given as:
// web.xml
<?xml version = "1.0" encoding = "UTF-8"?>
<web-app version = "3.1" xmlns = "http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <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>
    <session-config>
         <session-timeout>
             30
         </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file> jsp/first.jsp </welcome-file>
    </welcome-file-list>
</web-app>

Our application will run as follows

Figure: first run of application

 

   Figure: first run of application

Figure: waiting page (wait.jsp) is executed

Figure: waiting page (wait.jsp) is executed when authentication is under process. This page will automatically get refreshed after every 3 seconds. 

Figure: next.jsp after 3 second wait timing of wait.jsp

 

Like us on Facebook