Struts 2 execAndWait interceptor

The execute and wait interceptor is also known as execAndWait interceptor. The intermediate result is displayed with the help of this interceptor. The wait result is specified in the struts.xml file. Parameters defined with execAndWait interceptor are:

delay: It is used to specify starting delay.

delaySleepInterval: It is used to specify the time interval in a millisecond.

threadPriority: It is used to specify the priority of the thread.

Struts execAndWait Interceptor Example:

Create the index.html file containing link for executing intermediate action:

index.html file contains link to execute action before redirecting to next page. It is used to perform intermediate action between redirection.

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Welcome Page</title>
</head>
<body>
Welcome user
<br>
<a href="login">Action of printing numbers</a>
</body>
</html>

Create wait.jsp page showing intermediate result:

Create one JSP file showing intermediate result till particular action takes place in background. wait.jsp page display loading please wait…. till background action completes.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="refresh" content="4">
<title>Processing page</title>
</head>
<body>
loading please wait......
</body>
</html>

Create welcome.jsp page to display the final result:

In order to display final result after intermediate action, welcome.jsp file is created. This file displays result after printing numbers.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
welcome to the world of technology. 
<br>
Learn Here Lead Anywhere.....
</body>
</html>

Create action class LoginAction.java containing code for intermediate action:

Create one java class containing execute() method to mention intermediate action of printing numbers. It will print numbers from 0 to 1000 and then print done and then will redirect to the next resource.

public class LoginAction {
public String execute()
{
for(int i=0;i<=1000;i++)
{
System.out.println(i);
}
System.out.println("done");
return "success";
}
}

web.xml file is created inside WEB-INF folder in WebContent folder:

<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>StrutsExecandWaitInteceptor</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
<filter>  
  <filter-name>struts2</filter-name>  
   <filter-class>  
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
   </filter-class>  
  </filter>  
  <filter-mapping>  
   <filter-name>struts2</filter-name>  
    <url-pattern>/*</url-pattern>  
  </filter-mapping>  
</web-app>

Construct struts.xml file with the entry of Interceptor in it:

struts.xml file contains the entry of the action class, interceptor-ref name and result pages for success, error and wait.

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts  
Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  
<struts>  
<package name="default" extends="struts-default">  
  
<action name="login" class="LoginAction">  
<interceptor-ref name="completeStack"></interceptor-ref>
<interceptor-ref name="execAndWait"></interceptor-ref>
<result name="success">welcome.jsp</result>  
<result name="error">error.jsp</result>
<result name="wait">wait.jsp</result>
</action>  
  
</package>  
</struts> 

Output:

In order to run the application, right-click on the project -> Click on the option Run As -> then select Run on Server. It will show Welcome user and a link to Action of printing numbers.

Struts execAndWait interceptor

After clicking on the link, it will print loading please wait…. on the screen, and at the same time numbers from 0 to 1000 are printed on the console and at last done message is printed. Till done message is printed, loading please wait…. remains on the screen.

Struts execAndWait interceptor

After the done message is printed it will redirect to the next page i.e. welcome.jsp. It will print welcome to the world of technology and Learn Here Lead Anywhere..... on the screen.

Struts execAndWait interceptor