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.
Developers can create a custom interceptor in Struts2. We can implement the interceptor interface in our code to create a custom interceptor.
Parameters✨
There are three parameters in the custom interceptor. We will be discussing all three of them in this section. After that, we will discuss an example to understand the custom interceptors better.
-
init(): This is the most important parameter in custom interceptors and is used to initialize a new custom interceptor.
-
intercept(ActionInvocation invocation): This parameter is used to define the processing logic of the created custom interceptor. Since these are custom interceptors, they do not have pre-defined logic, and we need to assign a processing logic to the interceptor.
- destroy(): We can use this parameter to clean up all the resources used in the interceptor before destroying the custom interceptor.
Example👀
Now that we have discussed the theory associated with the custom Interceptors in Java, let us look at a basic example to understand it better. The structure of the project would look something like this,
Login.jsp
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>Custom Interceptor</title>
</head>
<body>
<h3>Custom Interceptor</h3>
<s:form action="Login">
<s:textfield name="userName" label="UserName" />
<s:submit value="Hello" align="center"/>
</s:form>
</body>
</html>
The login.jsp page is used to provide the user with a form to enter their username. The form is redirected to the login.java page for further processing.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts.xml
<struts>
<package name="user" extends="struts-default">
<interceptors>
<interceptor name="welcomeIntercepter" class="com.CodingNinjas.customintercepter.CustomIntercepter">
</interceptor>
</interceptors>
<action name="Login" class="com.CodingNinjas.action.Login">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="welcomeIntercepter"/>
<result name="success">/welcome.jsp</result>
</action>
</package>
</struts>
login.java
public class Login {
//Creating private data members
private String userName;
//Defining the logic for the interceptor
public String execute(){
System.out.println("Login's execute() " + "is called...");
return "success";
}
//Creating a function to get the values
public String getUserName() {
return userName;
}
//Creating a function to set the values
public void setUserName(String userName) {
this.userName = userName;
}
}
The Login.java page is used to fetch the name of the user. The fetched username is then displayed on the welcome.jsp page.
customInterceptor.java
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.opensymphony.xwork2.util.ValueStack;
public class CustomIntercepter implements Interceptor{
//Initialising the custom interceptor here using the init function
public void init() {
System.out.println("CustomIntercepter's " + "init methods is called.");
}
//Defining the logic of the custom interceptor using the intercept parameter
public String intercept(ActionInvocation actionInvocation) throws Exception {
System.out.println("Before " + "actionInvocation.invoke() called.");
ValueStack stack = actionInvocation.getStack();
String userName = stack.findString("userName");
stack.set("userName","Hi " + userName + ", welcome.");
String resultString = actionInvocation.invoke();
System.out.println("After " + "actionInvocation.invoke() called.");
return resultString;
}
//Reseting all the resources used during by the custom interceptor
public void destroy() {
System.out.println("CustomIntercepter's " + "destroy methods is called.");
}
}
The customInterceptor.java is used to declare the custom interceptor in this code. We first initialize the custom interceptor using the init() parameter. We then define the processing logic for the custom interceptor using the intercept() parameter. At last, we clean up all the resources used by the custom interceptor using the destroy() parameter. We will also trigger some command line outputs to demonstrate the execution flow of the code.
welcome.jsp
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>Custom Interceptor</title>
</head>
<body>
<h3>Custom Interceptor</h3>
<h4><s:property value="userName" /></h4>
</body>
</html>
We use the welcome.jsp page to display the username of the user with a welcome note.
Output
On Console
Now let's discuss some frequently asked questions associated with the custom interceptors.
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.
What is the advantage of using interceptors?
Interceptors are most commonly used for auditing, logging, and profiling. They are used in association with the Java EE merged classes.