As we know Struts lets us create web applications using servlet and JSP. Now we will talk about one of the useful features of struts2; interfaces. Interfaces in general have abstract methods without the body and static constants.
We implement these interfaces in a class and then provide body to the methods. Struts2 Aware interfaces help us to insert data into a request, response, context, or session object. There are four Aware interfaces. Through this blog, we will focus on learning the servletContextAware interface.
📌Aware Interfaces
First let us briefly see what all the four interfaces do.
SessionAware: Actions that need access to the user's HTTP session attributes should implement this interface.
ServletContextAware: The Action class must implement the ServletContextAware interface to save information in the application scope.
ServletRequestAware: All action classes that want to access the servlet request object should implement this interface.
ServletResponseAware: Defines an object that can let a servlet respond to the client.
The action class must implement all the Aware interfaces to hold information that may be obtained from other action classes.
📌Action class
Action classes are the ones that respond to the users actions. They decide what will Struts render for the user to view. The action class in struts 2 is POJO (Plain Old Java Object).
POJO signifies that we do not need to implement interfaces or extend classes.
An execute() method defines the business logic. The following is an example of an action class:
import com.opensymphony.xwork2.Action;
public class ActionExample
{
public String execute()
{
return SUCCESS; //SUCCESS is one of the five constants that can be
// returned from the action class
}
}
You can also try this code with Online Java Compiler
The ServletContextAware interface has a wide range of potential applications. They are as follows:
We may use a collection to contain all the records of a table in the ServletContext object and get this information from any action class. The web application's performance will improve as a result.
We may save the Connection object and get it from any action class.
📌Example of ServletContextAware
In the example, we will make two links: one to the first page, Store Data, and one to the second page Get Data. If we click directly on the second page, no data will display, but if we first click on the first link(Store Data), data gets stored in the ServletContext object and may be retrieved from another action class. The second link(Get Data) retrieves data from the ServletContext object.
For this example, we will create the following files:
Index.jsp - This will provide connections to the first and second actions.
Struts.xml - It will define the outcome and action.
StoreData.java - For saving data in the ServletContext object.
GetData.java - To retrieve data from the ServletContext object.
StoreDataSuccess.jsp, GetDataSuccess.jsp and GetDataError.jsp - For displaying the results.
Now let us create all of the files mentioned above one by one.
📃Index.jsp
This JSP file generates two links, the first executing the first action class and the second invoking the second action class.
This class will retrieve the data from the application scope. If any data with the login name is present in the session scope, it returns success; else, it returns an error.
This is one of the three view components. This file will create the login form.
<jsp:include page="Index.jsp"></jsp:include>
<%@ taglib uri="/struts-tags" prefix="s" %>
Hello user, data is set(saved) into the ServletContext object.
<br/>
Now you may click the second page to verify if the data is set or not.
<br/>
Data is :<s:property value="#application.object1"/>
📃GetDataSuccess.jsp
This is the second view component. This page displays the welcome message along with the username.
<jsp:include page="Index.jsp"></jsp:include>
<%@ taglib uri="/struts-tags" prefix="s" %>
Hello! The data is available.<br/>
Data is :<s:property value="#application.object1"/>
📃GetDataError.jsp
This third view component displays the error message if the data is not set.
<jsp:include page="index.jsp"></jsp:include>
Sorry. The data is not set in the ServletContext object, so it is not retrieved.
They help us to insert data into a request, response, context, or session object.
How many different types of Aware interfaces are there?
There are four types of Aware interfaces which are SessionAware, ServletContextAware, ServletRequestAware, and ServletResponseAware interface.
What is the purpose of the ServletContextAware interface?
It can help in containing all the records of a table in the ServletContext object and get this information from any action class. We can also save the Connection object and get it from any action class.
What is the benefit of importing the Action interface?
If we implement the Action interface, we can directly use any of the five constants it provides instead of values.
Conclusion
By using the struts2 framework, we can create MVC-based Java web applications. Through this blog, we learned what an Aware interface is and its types. We also learned the purpose of an action class. We discussed in detail about ServletContextAware interface through an example by creating the XML, java, and JSP files.