ServletContextAware Interface Example

The ServletContextAware interface must be executed by the Action class to store the data in the application scope.

It contains just a single strategy setServletContext. Syntax:

  1. public void setServletContext(ServletContext context);  

Usage of ServletContextAware interface

There can be a great deal of genuine use of ServletContextAware interface. They are:

  • You can hold every one of the records of a table in the ServletContext object utilizing accumulation and get these data from any activity class. Along these lines, execution of the web application will be increased.
  • You can store the Connection object and get the association object from any activity class etc.

Example of ServletContextAware interface

In thie model, we are making two connections first page and second page. On the off chance that you direct snap on the second page, it won't demonstrate any information yet on the off chance that you have tapped on the principal connect, information will be put away in the ServletContext item and it very well may be acquired from another activity class. In the second connection, we are getting information put away in the ServletContext object.

In this precedent, we are need following pages

  1. index.jsp for giving connects to the first and second action.
  2. struts.xml for characterizing the outcome and action.
  3. FirstAction.java for putting away information in the ServletContext object.
  4. SecondAction.java for getting information from the ServletContext object.
  5. View components for the displaying results.

Example of ServletContextAware interface

In thie model, we are making two connections first page and second page. In the event that you direct snap on the second page, it won't demonstrate any information however on the off chance that you have tapped on the principal interface, information will be put away in the ServletContext article and it tends to be acquired from another activity class. In the second connection, we are getting information put away in the ServletContext object.

In this precedent, we are need following pages

  1. index.jsp for giving connects to the first and second action.
  2. struts.xml for characterizing the outcome and action.
  3. FirstAction.java for putting away information in the ServletContext object.
  4. SecondAction.java for bringing information from the ServletContext object.
  5. View components for the displaying results.

1) Create index.jsp for information

This jsp page makes two connections, first connection summons first activity class and second connection conjures second activity class.

index.jsp
  1. <hr/>  
  2. <a href="first">first page</a>|  
  3. <a href="second">second page</a>  
  4. <hr/>  

2) Define action and result in struts.xml

This xml file defines one package and 2 actions.

struts.xml
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD   
  3. Struts Configuration 2.1//EN"   
  4. "http://struts.apache.org/dtds/struts-2.1.dtd">  
  5.   
  6. <struts>  
  7.   
  8. <package name="abcd" extends="struts-default">  
  9. <action name="first" class="com.javacodegeeks.FirstAction">  
  10. <result>firstsuccess.jsp</result>  
  11. </action>  
  12.   
  13. <action name="second" class="com.javacodegeeks.SecondAction">  
  14. <result>secondsuccess.jsp</result>  
  15. <result name="error">seconderror.jsp</result>  
  16. </action>  
  17.   
  18. </package>  
  19. </struts>      

3) Create the action class to store data

This action class implements the ServletContextAware interface and overrides the setServletContext method to store the information in the application scope.

FirstAction.java
  1. package com.javacodegeeks;  
  2. import javax.servlet.ServletContext;  
  3. import org.apache.struts2.util.ServletContextAware;  
  4.   
  5. public class FirstAction implements ServletContextAware{  
  6.   
  7.     public void setServletContext(ServletContext ctx) {  
  8.         ctx.setAttribute("dummyobject","hello");  
  9.     }  
  10.       
  11.     public String execute(){  
  12.         return "success";  
  13.     }  
  14. }  

4) Create the action class to get data

This class gets the information from the application scope, if any information is found in the session scope with login name, it returns success otherwise false.

SecondAction.java
  1. package com.javacodegeeks;  
  2. import javax.servlet.ServletContext;  
  3. import org.apache.struts2.ServletActionContext;  
  4.   
  5. public class SecondAction{  
  6.     public String execute(){  
  7.         ServletContext ctx=ServletActionContext.getServletContext();  
  8.         String obj=(String)ctx.getAttribute("dummyobject");  
  9.         if(obj!=null){  
  10.             return "success";  
  11.         }  
  12.         else{  
  13.             return "error";  
  14.         }  
  15.     }  
  16. }  

5) Create view components

There are many view components:

  • firstsuccess.jsp
  • secondsuccess.jsp
  • seconderror.jsp
firstsuccess.jsp

This page creates the login form.

  1. <jsp:include page="index.jsp"></jsp:include>  
  2.   
  3. <%@ taglib uri="/struts-tags" prefix="s" %>  
  4. Hello user, data is set into the ServletContext object  
  5. <br/>  
  6. Now you may click the second page to verify  
  7. <br/>  
  8. Data is :<s:property value="#application.dummyobject"/>  
secondsuccess.jsp

This page prints the welcome message with the username.

  1. <jsp:include page="index.jsp"></jsp:include>  
  2. <%@ taglib uri="/struts-tags" prefix="s" %>  
  3. Hello user, data is found<br/>  
  4.   
  5. Data is :<s:property value="#application.dummyobject"/>  
seconderror.jsp

This page displays the error message.

  1. <jsp:include page="index.jsp"></jsp:include>  
  2. Sorry , data is not found in the  ServletContext object  





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition