• Post Reply Bookmark Topic Watch Topic
  • New Topic

ServletContextListener declared in TLD

 
Randy Gibbons
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am implementing the downloaded source code from Chapter 10 (The Application Events Framework) of Marty Hall's "more Servlets and JavaServer Pages." I'm using Tomcat 5.5.7. The initial example illustrating ServletContextListener works fine. But when I remove the declaration of the listener from web.xml to a tag library (company-name-taglib.tld, in the book's example), Tomcat is APPARENTLY not loading the listener or executing its contextInitialized() method. The .tld file is in WEB-INF. I am 99.9% sure my web.xml and .tld files are conforming (I'm certainly not getting any parse errors). And when I create my own analogous but extremely simple example declaring a ServletContextListener from a .tld rather than from the web.xml, it works fine. Has anyone tried Marty's examples? In any case, any hints as to what I should check?

I say APPARENTLY not loading/executing. It was first apparent simply from running the application (null values for attributes that the listener is supposed to initialize). When I try including a "hi it's me" system.out.println statement in the contextIntialized() code, I am finding it doesn't output to Tomcat's log, in this or any other case, though the same statements in my HttpSessionListener's do output to the log. Is this an expected result (system.out.println statements produce output for HttpSessionListener's but not ServletContextListener's) and, if so, why? I then tried writing out a physical file from within my contextInitialized() code instead, and that produces a file for all my ServletContextListener's except the case in point, which seems to confirm that that particular listener is not getting loaded/executed. So a secondary question is, what is the best way, at least in a Tomcat 5.x environment, for knowing that the listener is being loaded/executed (other than inferring this from the runtime results or from debugging via file output)?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting. I've never tried to add a listener to a TLD, but here's what the spec has to say:

A Container is required to locate all TLD files (see Section JSP.7.3.1 for details on how they are identified), read their listener elements, and treat the event listeners as extensions of those listed in web.xml.

The order in which the listeners are registered is undefined, but they are
registered before application start.


So, it should work.

If I get a chance, I'll play around this weekend with it and see what happens.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, on re-reading your post. You are getting a listener to work -- just not the example you are trying to emulate?

Are any tags in the same TLD being loaded?
[ July 22, 2005: Message edited by: Bear Bibeault ]
 
Randy Gibbons
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
Oh, on re-reading your post. You are getting a listener to work -- just not the example you are trying to emulate?

Are any tags in the same TLD being loaded?

[ July 22, 2005: Message edited by: Bear Bibeault ]


Yes, I did get a listener declared in a TLD to work, just not the example from Marty's book. That listener worked fine too, when declared in the web.xml. And yes, the tag declared in the same TLD is working.
[ July 22, 2005: Message edited by: Randy Gibbons ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!