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.
login-error.jsp
3.4.
login-success.jsp
3.5.
struts.xml
3.6.
web.xml
3.7.
Output
4.
Frequently Asked Questions
4.1.
What is the advantage of using the prepare interceptor?
4.2.
How can we use the prepare Interceptor?
4.3.
What are the extensions for the prepare interceptor?
5.
Conclusion
Last Updated: Mar 27, 2024
Medium

Prepare 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 prepare Interceptor is used to call the prepare method before the execute method is called. The prepare Interceptor is present in the preparable interface in Java.

Parameters

There is only one parameter in the prepare interceptor,

  • alwaysInvokePrepare: This parameter is set to true as a default value.

Example 👀

Now that we have discussed the theory associated with the prepare 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.prepare;


public class Login implements Preparable{  
	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 void prepare() throws Exception {  
	
    		System.out.println("preparation logic");  
	}  
  	
	public String execute(){  
	
    		System.out.println("actual logic");  
    		return "success";  
	}  
}  

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.

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>
	<constant name="struts.devMode" value="true"></constant>
	<package name="abc" extends="struts-default" >
		<action name="login" class="com.codingNinjas.Login" method="execute">
			<interceptor-ref name="params"></interceptor-ref>
			<interceptor-ref name="prepare"></interceptor-ref>
			<result name="success" type="dispatcher">/success.jsp</result>
			<result name="error">/error.jsp</result>
		</action>
	</package>
</struts> 

 

web.xml

?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>StrutsPrepInteceptor</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>

 

Output

output

 

output

Frequently Asked Questions

What is the advantage of using the prepare interceptor?

The prepare interceptor comes in handy when the logic needs to run before the actual execute method is run.

How can we use the prepare Interceptor?

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

What are the extensions for the prepare interceptor?

As of now, there are no extensions for the prepare interceptor in Java.

Conclusion

This Blog covered all the necessary points about the prepare Interceptors in Java. We also looked at the different parameters present in the prepare Interceptor. At last, we looked at a code to understand the prepare 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