Monday, 13 July 2015

How to use ServletRequestAttributeListener 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 then we can
use "ServletRequestAttributeListener". In short web application receives notification as and when any operation is done on the attribute of a ServletRequest.

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

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

Here are the steps to implement "ServletRequestAttributeListener"

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

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

Here we assume that we have created a class with the name "MyServletRequestAttributeListener" which implements "ServletRequestAttributeListener"

2) Add a class which implements "ServletRequestAttributeListener"

Here is the code snippet for the  quick reference:

public class MyServletRequestAttributeListener implements ServletRequestAttributeListener {

    public MyServletRequestAttributeListener() {
    }

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

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

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


3) Run and Verify

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

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

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

When "doGet" method of the "StoreInstitute" 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 Request (Name -> institute, Value ->ABC)
Attribute Added in Request (Name -> address, Value ->India)
Attribute Replaced in Request (Name -> institute, Value ->ABC)
Attribute Removed in Request (Name -> address, Value ->India)


Cheers!
Henal Saraiya
(Lead Consultant)
CIGNEX Datamatics 

No comments:

Post a Comment