Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction💁
2.
Parameters✨
3.
Example 👀
3.1.
index.jsp
3.2.
Login.java
3.3.
intermediatepage.jsp
3.4.
login-error.jsp
3.5.
login-success.jsp
3.6.
Output
4.
Frequently Asked Questions
4.1.
What is JavaFX, and how can we run a JavaFX code?
4.2.
What is Apache Struts 2?
4.3.
What are threads?
4.4.
What is Apache-tomcat?
4.5.
How can we download Apache-tomcat, and what is the default port Apache-tomcat?
5.
Conclusion
Last Updated: Mar 27, 2024
Medium

execAndWait Interceptor

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,

Project Structure

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

Login Page
Processing Page
Success Login

 

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.

Conclusion

This Blog covered all the necessary points about the execAndWait Interceptors in Java. We also looked at the different parameters present in the execAndWait Interceptor. At last, we looked at a code to understand the execAndWait Interceptors in Java.

Hey Ninjas! Don’t stop here. Check out Coding Ninjas for Python, more unique courses, and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and excellent Machine Learning and Python problems.

Live masterclass