Tuesday, 14 July 2015

How to use HttpSessionAttributeListener in Java EE

In dynamic web application if we want to keep track or we want to do some operation like log the attribute which is being added, replaced, deleted in HttpSession then we can
use "HttpSessionAttributeListener". In short web application receives notification as and when any operation is done on the attribute of a HttpSession.

There is just a single listener which keeps track of the attribute being added, deleted, replaced by any of the Servlet.

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

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

Here are the steps to implement "HttpSessionAttributeListener"

1) Modify web.xml (deployment descriptor) file to add listener
2) Add a class which implements "HttpSessionAttributeListener"
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 "HttpSessionAttributeListener" we need to add below
lines in it:

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

Here we assume that we have created a class with the name "MyHttpSessionAttributeListener" which implements "HttpSessionAttributeListener".

2) Add a class which implements "HttpSessionAttributeListener"

Here is the code snippet for the  quick reference:

public class MyHttpSessionAttributeListener implements HttpSessionAttributeListener {

    public MyHttpSessionAttributeListener() {
    }

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

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

    public void attributeReplaced(HttpSessionBindingEvent arg0) {
        System.out.println("Attribute Replaced in session (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 Session scope.


3) Run and Verify

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

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(); // Line #1
session.setAttribute("name", "Peter"); // Line #2
session.setAttribute("name", "John"); // Line #3
session.removeAttribute("name"); // Line #4
 }

Now to verify our listener, we need to call our "StudentLogin" servlet in which we have done operation on the attribute of the "HttpSession".

In "StudentLogin",

Line #1 takes the HttpSession object from the HttpServletRequest object.
Line #2 sets the attribute in HttpSession scope.
Line #3 replaces the attribute from the HttpSession scope.
Line #4 removes the attribute from the HttpSession.

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

Jul 14, 2015 6:24:21 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 312 ms
Attribute Added in session (Name -> name, Value ->Peter)
Attribute Replaced in session (Name -> name, Value ->Peter)
Attribute Removed in session (Name -> name, Value ->John)


Cheers!
Henal Saraiya
(Lead Consultant)
CIGNEX Datamatics

No comments:

Post a Comment