• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A Question about ServletRequestListener behaviour

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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





 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 71
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 70
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic