Hi Mark,

I have tried using a servlet context listener in Tomcat and it behaves just as Yoav says it does.  The contextInitialized() gets called first and then an initialization servlet which I have set to <load-on-startup>1</load-on-startup> gets run.  So, at least Tomcat works this way.  I wonder if you are using an older version of Resin than the version where Yoav inspected the code?

One thing I might be in disagreement with Yoav is that I don't think the SRV 10.2.2 section make it perfectly clear that the contextInitialized() gets run before servlet init() methods.  It leads you to assume that the contextInitialized() will get called before the servlet's init() method, but does not say so explicitly. 

SRV 10.3.3 does say something more explicitly about this....

"
The web container creates an instance of each listener class and registers it for event
notifications prior to the processing of the first request by the application. The web
container registers the listener instances according to the interfaces they implement
and the order in which they appear in the deployment descriptor. During web
application execution listeners are invoked in the order of their registration.
"

However, all that says it that the contextInitialized() method for all servlet context listeners finishes before accepting the first request.  I define "request" as a called to service() or, more commonly, doGet(), doPost(), etc....  Calling a servlet's init() method is *not* a "request", per-say.  So, I think it is possible that a container implementor might interperet the spec to say that service() can't be called before contextInitialized() is done but they might not put any constraint on init() not being called until contextInitialized() is done.  I think that would be a bad interperetation of the spec but technically legitimate, nevertheless.

Yoav, any comments about that?

BTW, here is a nice Listener class which provides some useful debugging for checking on the loading order of these things.  I didn't write it.  I was posted to the Tomcat list.  I think it will be useful, though.  See attached zip file.


Jake

At 09:37 PM 12/12/2002 -0800, you wrote:
Yoav & Jacob,

> >Are you sure about that?
>
> Positive.  The example in SRV 10.2.2 makes this clear.  I've also
> inspected the code in tomcat and resin myself and they both implement
> the spec the same way.
>
> >contextInitialized() get called when everything has gotten done
> >initializing.  That is, the webapp is now ready to run.  So,
> in effect,
> a
> >servlet with <load-on-startup>1</load-on-startup> will get called
> *before*
> >the contextInitialized() method gets called because until
> that servlet
> >completes the init(), the webapp is *not* initialized.
>
> No.  Any servlet init() methods are called after contextInitialized().

I created a ServletContextListener that initializes log4j in
contextInitialized() and shuts it down in the contextDestroyed().  I also
have a basic servlet that calls log4j in the init() method (but does not do
any initialization).

I deploy these in resin 2.1.6 with the following web.xml snippet:

    <context-param>
        <param-name>trace-configuration-file</param-name>
        <param-value>WEB-INF/log4j-configuration.xml</param-value>
    </context-param>
   
    <listener>
        <listener-class>
 
com.bevocal.application.platform.trace.log4j.Log4jServletContextListener
        </listener-class>
    </listener>
   
    <!-- trace configuration servlet -->
    <servlet>
        <servlet-name>trace</servlet-name>
 
<servlet-class>com.bevocal.application.platform.trace.log4j.Log4jConfigServl
et</servlet-class>

        <load-on-startup>1</load-on-startup>
    </servlet>

The behavior I am seeing is the servlet init method appears to get called
(and I get a "log4j not initialized" error message) and THEN the
ServletContextListern contextInitialized() method gets called.  If I remove
the load-on-startup element, then I do not get the "log4j not initialized",
the contextInitialized() method is called, and when I access the servlet
then it's init method outputs a proper log message.

I don't know if this behavior is specific to resin or configurable within
resin, but not the behavior I am expecting given the previous discussion on
this thread.  So, did I miss something critical here?  It does not appear
that contextInitialized() is called before servlet init() methods if they
are configured to load on startup.  At least in resin.  Can someone try this
in Tomcat?

-Mark

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Attachment: TestListener.zip
Description: Zip archive

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>


Reply via email to