Saturday, 11 July 2015

When to use ServletContextAttributeListener in java

In dynamic web application when we want to share data among servlet then we need to take help of "ApplicationContext" scope. Every servlet can get the object of "ServletContext"
and store data in it for other servlet to leverage it. If we want to keep track or we want to do some operation like log the attribute which is being added, replaced, deleted then we can
use "ServletContextAttributeListener".

"ServletContextAttributeListener" has below three methods which will be triggered at the time of attribute added, replaced and deleted respectively from and to "ServletContext" scope.

1) public void attributeAdded(ServletContextAttributeEvent arg0)
2) public void attributeReplaced(ServletContextAttributeEvent arg0)
3) public void attributeRemoved(ServletContextAttributeEvent arg0)


Here are the steps to implement "ServletContextAttributeListener"

1) Modify web.xml (deployment descriptor) file to add listener
2) Add a class which implements "ServletContextAttributeListener"
3) Run and Verify

Lets see the steps in detailed:

1) Modify web.xml (deployment descriptor) file to add listener

web.xml file will be read / looked upon by the container while loading any of the project. To inform container about "ServletContextAttributeListener" we need to add below
lines in it:

<listener>
<listener-class>com.listener.ApplicationContextAttributeListener</listener-class>
</listener>

Here we assume that we have created a class with the name "ApplicationContextAttributeListener" which implements "ServletContextAttributeListener"

2) Add a class which implements "ServletContextAttributeListener"

Here is the code snippet for the  quick reference:

public class ApplicationContextAttributeListener implements ServletContextAttributeListener {

    /**
     * Default constructor. 
     */
    public ApplicationContextAttributeListener() {
        // TODO Auto-generated constructor stub
    }

    public void attributeAdded(ServletContextAttributeEvent arg0) {
    System.out.println("Attribute Added in Context (Name -> " + arg0.getName() + ", Value ->" + arg0.getValue() + ")");
    }

    public void attributeReplaced(ServletContextAttributeEvent arg0) {
    System.out.println("Attribute Replaced in Context (Name -> " + arg0.getName() + ", Value ->" + arg0.getValue() + ")");
    }

    public void attributeRemoved(ServletContextAttributeEvent arg0) {
    System.out.println("Attribute Removed in Context (Name -> " + arg0.getName() + ", Value ->" + arg0.getValue() + ")");
    }
}

As we can see there are three methods "attributeAdded","attributeReplaced" and "attributeRemoved" which will be called at the time of attribute being added,replaced,removed
from and to the ServletContext scope.


3) Run and Verify

To verify same lets create a dummy servlet called "StoreStudentData" which has a below "doGet" method.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
getServletContext().setAttribute("institute", "ABC"); //Line #1
getServletContext().setAttribute("address", "India"); //Line #2
getServletContext().setAttribute("institute", "XYZ"); //Line #3
getServletConfig().getServletContext().removeAttribute("address"); //Line #4
}

Now to verify our listener, we need to call our "StoreStudnetData" servlet in which we have done operation on the attribute of the "ServletContext".
In "StoreStudentData", Line #1 & #2 adds an attribute. Line #3 replaces attribute added in Line #1. Line #4 removes attribute.

When "doGet" method of the "StoreStudentData" servlet gets called then inside log we can see below entries:

Jul 10, 2015 6:24:21 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 312 ms
Attribute Added in Context (Name -> institute, Value ->ABC)
Attribute Added in Context (Name -> address, Value ->India)
Attribute Replaced in Context (Name -> institute, Value ->ABC)
Attribute Removed in Context (Name -> address, Value ->India)


Cheers!
Henal Saraiya
(Lead Consultant)
CIGNEX Datamatics

No comments:

Post a Comment