Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction💁
2.
Example 👀
2.1.
index.jsp
2.2.
Login.java
2.3.
User.java
2.4.
login-error.jsp
2.5.
login-success.jsp
2.6.
struts.xml
2.7.
Output
3.
Frequently Asked Questions
3.1.
What is JavaFX, and how can we run a JavaFX code?
3.2.
What is Apache Struts 2?
3.3.
How can we use the modelDriven Interceptor?
3.4.
What is Apache-tomcat?
3.5.
How can we download Apache-tomcat, and what is the default port Apache-tomcat?
4.
Conclusion
Last Updated: Mar 27, 2024
Medium

modelDriven 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 modelDriven Interceptor is used to make other objects of the model as default objects for the value stack. The modelDriven Interceptor is present in the Apache Struts 2 framework.

There are no defined parameters for the modelDriven Interceptors.

Example 👀

Now that we have discussed the theory associated with the modelDriven Interceptor, let us look at a basic example to understand it better. The structure of the project would look something like this,

Structure

index.jsp

<%@ taglib uri="/struts-tags" prefix="s" %>


<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;
import com.opensymphony.xwork2.ModelDriven;


public class Login implements ModelDriven<User>{
	private User user;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	public User getModel(){
		user=new User();
		return user;
	}
	public String execute(){
		if(user.getPassword().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.

User.java

package com.codingNinjas;


public class User {
	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;
	}


}

This is the code that is used to create a model for the user.

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.

struts.xml

<?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="abc" extends="struts-default" >
		<action name="login" class="com.codingNinjas.Login">
			<result name="success" >/login-success.jsp</result>
			<result name="error">/login-error.jsp</result>
		</action>
	</package>
</struts>    

Output

Output-1

 

Output-2

 

Must Read 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.

How can we use the modelDriven Interceptor?

The modelDriven Interceptor is present in the ModelDriven Interface. Thus before we can start using the modelDriven Interceptor in our code, we need to implement the ModelDriven interface.

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 modelDriven Interceptors in Java. We also looked at the different parameters present in the modelDriven Interceptor. At last, we looked at a code to understand the modelDriven 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