File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes ServletContextListener and load-on-startup Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "ServletContextListener and load-on-startup" Watch "ServletContextListener and load-on-startup" New topic
Author

ServletContextListener and load-on-startup

Peter Loew
Greenhorn

Joined: Jul 06, 2003
Posts: 27
Hi Guys,

I am using a ServletContextListener and everything works fine. The problem arises when I add the <load-on-startup>1</load-on-startup> in the DD.

After I add this line in the DD my code does not appear to run beyond the ServletContextListener. But the strange thing is that when I hit the servlet I get an Error 500 message detailing a NullPointerException in my servlet, meaning that the Servlet has started up.

I think the servlet is starting up BEFORE the ServletContextListener code is run - but that would defy the whole point of the listener wouldn't it?

Any thoughts to what's happening here?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Just to cover all the bases.
That load-on-startup tag is inside a servlet declaration, right?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Peter Loew
Greenhorn

Joined: Jul 06, 2003
Posts: 27
Yes it is:

Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Have you tried putting print statements in the context listener and the init method of the servlet to see which is starting first?

NOTE: you may want to include a timestamp in the lines, not all logging packages guarantee that the lines will be printed in the order they occur.
Peter Loew
Greenhorn

Joined: Jul 06, 2003
Posts: 27
Ben,

I have just confirmed it with timestamps and yes, the servlet init() method is getting called before the ServletContextListener code.

I've just noticed, that once I've started the container nothing happens until I first hit the servlet with the browser, then the init() method seems to kick off, and then the ServletContextListener.

Now, isn't the servlet supposed to load after the container has started up given its "load-on-startup" is set to 1? Strange indeed.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

What container are you using?
Peter Loew
Greenhorn

Joined: Jul 06, 2003
Posts: 27
I have tested this on both standalone OC4J and Oracle 9i Application Server. Same results.
Peter Loew
Greenhorn

Joined: Jul 06, 2003
Posts: 27
Well look what I found here:

http://www.coderanch.com/t/165335/java-Web-Component-SCWCD/certification/events-startup-webapp

Accoding to this post the init() method does get called before any context listeners. Which means that the context gets created before the context listener runs. I would have thought that it was the other way round no?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

According to the spec, contextInitialized should be fired before any servlet initializations.

Peter Loew
Greenhorn

Joined: Jul 06, 2003
Posts: 27
Hmm... that's exactly how I expected the spec to read. There seems to be a lot of conflicting information going around about this issue.

Seems like it could be a container issue but I find that hard to believe given that 9iAS is an enterprise level application server.

I'll see what info I can find on Oracle's site. But in the mean time a good test would be to set up a simple servlet with a context listener and debug out statements and see the chronological chain of events. If I have time I might try this with another container.

What do you think it could be Ben?
Neeraj Dheer
Ranch Hand

Joined: Mar 30, 2005
Posts: 225
why dont you post/send me your code and ill try it out on Tomcat and Websphere..
i am sorry for not being able to write it myself, am travelling and done have access to a PC at the place i am put up at the moment...but if you could post the code here, ill try it out at work...
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Ben Souther:
Have you tried putting print statements in the context listener and the init method of the servlet to see which is starting first?

NOTE: you may want to include a timestamp in the lines, not all logging packages guarantee that the lines will be printed in the order they occur.



Originally posted by Peter Loew:
But in the mean time a good test would be to set up a simple servlet with a context listener and debug out statements and see the chronological chain of events. If I have time I might try this with another container.


Is there an echo in here???
Peter Loew
Greenhorn

Joined: Jul 06, 2003
Posts: 27
Originally posted by Ben Souther:


Is there an echo in here???


Ben,

I meant some different code (I am working on an application which is what I am having the trouble with). Of course I have debugged it as I said earlier, and the results were that the init() method gets called before the contextListener.

I will knock up a different, simple application just to test this feature on different containers.
Peter Loew
Greenhorn

Joined: Jul 06, 2003
Posts: 27
Originally posted by Neeraj Dheer:
why dont you post/send me your code and ill try it out on Tomcat and Websphere..
i am sorry for not being able to write it myself, am travelling and done have access to a PC at the place i am put up at the moment...but if you could post the code here, ill try it out at work...


Thanks Neeraj, I'll try to knock something up tomorrow and send you the ear/war file.
Peter Loew
Greenhorn

Joined: Jul 06, 2003
Posts: 27
Problem solved.

Here at work we use the aforementioned containers and application servers, which are based on the J2EE 1.3 spec, which in turn incorporates the Servlet 2.3 spec, which in turn, has no rules about the web application order of deployment.

The J2EE 1.4 spec, incorporating the Servlet 2.4 spec however, does indeed have rules about the order of deployment, as Ben posted and as I should have realised previously (I never really looked at the specs before, but now I realise how important and practical they really are from a developer POV)

So, I downloaded the latest OC4J 10g DP version which is officially J2EE 1.4 based, deployed my app and hey presto, the debug statements are in nice chronological order with the Servlet 2.4 spec and everything works smoothly.

Thanks all for the help on this.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Glad you got it working.
Thanks for posting back.
Neeraj Dheer
Ranch Hand

Joined: Mar 30, 2005
Posts: 225
that was very interesting...thanks for letting us know...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ServletContextListener and load-on-startup