Introduction💁
Java is an object-oriented, class-based, high-level programming language. It is based on the concept of WORA (write once use anywhere) for developer ease. Java classes are compiled into byte code. The compiled byte codes can be used on any platform supporting Java without recompiling the class.
The execAndWait Interceptor is also known as the execute and wait. It is used to display intermediate results. Developers can specify the wait time in the struct.xml file.
Parameters✨
There are three parameters that are defined for the execAndWait Interceptor,
- delay: The delay parameter is used to specify the delay time. By default, the initial delay time is zero.
- threadPriority: This parameter specifies the priority of each thread.
- delaySleepInterval: This parameter can only be used with the delay parameter. It is used to specify a delay time to check whether the background process is completed. By default, the value for the delaySleepInterval is 100 milliseconds.
Example 👀
Now that we have discussed the theory associated with the execAndWait Interceptor let us look at a basic example to understand it better. The structure of the project would look something like this,
index.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<s:form action="login">
<s:textfield name="name" label="Name"></s:textfield>
<s:password name="password" label="Password"></s:password>
<s:submit value="login"></s:submit>
</s:form>
The index.jsp file is used to create a basic form.
Login.java
package com.codingninjas;
public class Login{
private String name,password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute(){
try{Thread.sleep(2000);}catch(Exception e){}
if(password.equals("admin")){
return "success";
}
else
return "error";
}
}
The Login.java page is used to fetch the name and the password from the index.jsp page. Then these details are verified against a given set of conditions. If the conditions are satisfied, the user is directed to the success page. Else the user is directed to the error page.
intermediatepage.jsp
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<title>wait</title>
<meta http-equiv="refresh" content="0.5;url='<s:url/>'">
</head>
<body>
<p>your request is processing...</p>
<img src="processing.gif"/>
</body>
</html>
This is the code that actually uses the execAndWait Interceptor and holds the request for 0.5 seconds.
login-error.jsp
<p>Incorrect Password!!</p>
<jsp:include page="index.jsp"></jsp:include>
The user is redirected to this page in case the correct credentials are not entered.
login-success.jsp
<%@ taglib uri="/struts-tags" prefix="s" %>
Welcome, <s:property value="name"/>
In case the user provides correct credentials, they are redirected to this page.
Output
Related Article Apache Server, Why is Java Platform Independent
Frequently Asked Questions
What is JavaFX, and how can we run a JavaFX code?
JavaFX is a library present in Java that is used to create dynamic web pages as well as rich web applications. JavaFX applications can run on various platforms, including mobile, web, and desktop. JavaFX codes can be run on various platforms. But the two most commonly used platforms for JavaFX are NetBeans and Eclipse.
What is Apache Struts 2?
Apache Struts 2 is used for developing Java EE web applications. It is an open-source web application framework. Apache Struts 1 extends and uses the Java Servlet API. The Apache Struts 2 is an enhancement over the Apache Struts 1.
What are threads?
In Computer Science, threads can be understood as the smallest sequence of instructions that can be managed by a developer independently. Simply put, a task is divided into smaller independent tasks, and each smaller task is known as a thread.
What is Apache-tomcat?
Apache-tomcat is an open source and free HTTP web server environment which is used to run and compile Java codes. Apache-tomcat is maintained and developed by an open group of developers.
How can we download Apache-tomcat, and what is the default port Apache-tomcat?
We can download the latest stable version of Apache-tomcat from the official Apache-tomcat website. Also, the default port for Apache-tomcat is 8080. Users can validate the connectivity of the Apache-tomcat server by going to https://localhost:8080.