Sunday, 12 July 2015

When to use ServletRequestListener in java

In a dynamic web application if we want to track when any request is initialized or destroyed then "ServletRequestListener" listener we need to use.

"ServletRequestListener" has below two methods which will be triggered at the time of request is destroyed and request is initialized.

1) public void requestDestroyed(ServletRequestEvent arg0)
2) public void requestInitialized(ServletRequestEvent arg0)

Here are the steps to implement "ServletRequestListener"

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

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

Here we assume that we have created a class with the name "MyServletRequestListener" which implements "ServletRequestListener"

2) Add a class which implements "ServletRequestListener"

Here is the code snippet for the  quick reference:

public class MyServletRequestListener implements ServletRequestListener {

    public MyServletRequestListener() {
    }

    public void requestDestroyed(ServletRequestEvent arg0) {
        System.out.println("Hey request is destroyed " + arg0.getServletContext().getAttribute("studentLocation"));
System.out.println("Hey request is destroyed " + arg0.getServletRequest().getParameter("institute"));
    }

    public void requestInitialized(ServletRequestEvent arg0) {
        System.out.println("Hey request is initialized" + arg0.getServletRequest().getParameter("institute"));
        arg0.getServletContext().setAttribute("studentLocation ", "India");
    }
}


As we can see there are two methods "requestDestroyed" and "requestInitialized" which will be called at the time of servlet request gets destroyed and servlet request
is initialized respectively.

3) Run and Verify

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

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().print(getServletContext().getAttribute("institute"));
}

Now to verify our listener, we need to call our "FetchStudentData" servlet in which we have read the value of the "institute" which is passed in the Url.

Here is the log we can see while calling Url : http://localhost:8080/student/FetchStudentData?institute=Australia

Hey request is initialized Australia //Line #1
Hey request is destroyed India //Line #2
Hey request is destroyed Australia //Line #3

We can see that

Line #1 prints the value of "institute" which we have passed in the Url as by that time request was initialized. (Via "requestInitialized" method of the listener)

Line #2 prints the value of the "studentLocation" which is stored in the "ServletContext" (During "requestInitialized" method in a listener).

Line #3 prints the value of the request parameter "institute". (Via "requestDestroyed" method of the listener).

Cheers!
Henal Saraiya
(Lead Consultant)
CIGNEX Datamatics

No comments:

Post a Comment