wood burning stoves 2.0*
The moose likes Servlets and the fly likes A Question about ServletRequestListener behaviour Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "A Question about ServletRequestListener behaviour " Watch "A Question about ServletRequestListener behaviour " New topic
Author

A Question about ServletRequestListener behaviour

Parthiban Malayandi
Ranch Hand

Joined: Apr 15, 2008
Posts: 70
Hi all,

I thought ServletRequestListener's requestIntialized and requestDestroyed methods will be invoked whenever a request comes to Servlet but it is also invoked whenever I refresh or reload a static page .
Is that what it's indented to do ? I mean for any request ?(even for static page request). if so why it is not invoked when I first load that static page ?

Thanks in advance





Amol Nayak
Ranch Hand

Joined: Oct 26, 2006
Posts: 218
The requestInitialized and requestDestroyed will be called only when a new ServletRequest object is created/destroyed by the container. The object will be created only if the reqest if for a Dynamic resource (JSP or servlet) and not for any static resource on the Server.

Can you give some more details about your analysis done where by you came to this conclusion?

Share the following if possible
Some logs, Sequence of Events fired by you, URL Refreshed, web.xml and some relevant code from your application.

Kris Schneider
Ranch Hand

Joined: Feb 14, 2001
Posts: 71
Amol Nayak wrote:The object will be created only if the reqest if for a Dynamic resource (JSP or servlet) and not for any static resource on the Server.


Maybe. The API docs are a bit more specific:

A ServletRequestListener can be implemented by the developer interested in being notified of requests coming in and out of scope in a web component. A request is defined as coming into scope when it is about to enter the first servlet or filter in each web application, as going out of scope when it exits the last servlet or the first filter in the chain.

So, depending on how you define "static resource", having filters or servlets operate on the request will cause a request listener to be created...
Amol Nayak
Ranch Hand

Joined: Oct 26, 2006
Posts: 218
Kris Schneider wrote:
So, depending on how you define "static resource", having filters or servlets operate on the request will cause a request listener to be created...


Are you saying there are explicit mappings to static resources like *.jpg, *.bmp , *.html etc to some servlets and filters? Well that is unlikely, even if you do, the request will be intercepted by the Servlet/Filter and the requested resource will be served to you only if the Servlet/Filter has the logic to do so.

Having a look at the web.xml can give us some clues.
Kris Schneider
Ranch Hand

Joined: Feb 14, 2001
Posts: 71
Amol Nayak wrote:Are you saying there are explicit mappings to static resources like *.jpg, *.bmp , *.html etc to some servlets and filters? Well that is unlikely, even if you do, the request will be intercepted by the Servlet/Filter and the requested resource will be served to you only if the Servlet/Filter has the logic to do so.


I guess my point is that trying to use the concept of "static resources" to understand why a request listener is getting created isn't helpful. The API docs define exactly the conditions under which it happens.

Amol Nayak wrote:Having a look at the web.xml can give us some clues.


Sure, in addition to finding out what the actual request for the "static page" looks like (path, params, etc.).
Parthiban Malayandi
Ranch Hand

Joined: Apr 15, 2008
Posts: 70
Hi Amol and Kris,

Thanks for your repllies. I'm sharing the source codes so that it will be helpful to clear my doubt.

web.xml
=======



MyServletRequestListener
========================


Static Page which calls the Servlet when Button is clicked
==========================================================



Following is the Servlet which will be called by simple.html
=============================================================



Initially when I hit the page (something like this http://localhost:8080/exp/simple.html) I didn't get any log from calling servlet(SimpleServlet) and SerlvetListener(MyServletRequestListener) but when I click the
button I got the following output in log as expected




but Following is the log I get whenever I refresh or reload a page



from that log it's clear that request didn't hit Servlet(named SimpleServlet) but the listener is invoked.

So please clarify me.

Thanks in advance
Kris Schneider
Ranch Hand

Joined: Feb 14, 2001
Posts: 71
Parthiban Mahiby wrote:So please clarify me.


There are a few things you really should change, like not really sending a response from the servlet, but add a little more detail to your logging first:

Amol Nayak
Ranch Hand

Joined: Oct 26, 2006
Posts: 218
ok..

I have have tried deploying the web app provided on JBOSS which uses tomcat web container and on Websphere 7 and surprisingly (for me atleast) the ServletRequestListener's methods are invoked even for static resources in the application

Below are the logs from JBOSS




and below are the logs from Websphere 7



After having a look at a how tomcat works (based on some google search and the architecture docs i found)


There is one connector who listens to the Socket for incoming requests.

It then hands over the request to the HttpProcessor class which creates the ServletRequest for the incoming request, irrespective of whether it is a static or a Dynamic resource. Now since a Request object is created the Listener's methods gets invoked.

The Request is then Forwarded to the Servlet container or the Static resource processor (may not be the actual terms used for tomcat) and the appropriate response is then sent to the client.

This is new to me and found it confusing after reading the description of the API doc where one may think the listeners are notified only upon invocation of dynamic resources

Would be interesting to see somebody else's view/opinion on this.
Kris Schneider
Ranch Hand

Joined: Feb 14, 2001
Posts: 71
Tomcat contains something like the following in conf/web.xml (inherited by all apps):



Which basically means that there's always a servlet operating on any request to an app...regardless of what kind of resource it might be classified as...
Parthiban Malayandi
Ranch Hand

Joined: Apr 15, 2008
Posts: 70
Hi Amol and Kris,

First of all sorry for the delayed response. I too tried (using Tomcat ) by appending the extra code that Kris mentioned and came to know that ServletRequestListener is invoked even for static resources.

Thanks for clearing my doubt.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A Question about ServletRequestListener behaviour
 
Similar Threads
Servlet to applet communication
Static Methods and Instance Methods
jsp and database
doPost getting invoked twice
Session Attributes