• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

ServletContextListener and load-on-startup

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to cover all the bases.
That load-on-startup tag is inside a servlet declaration, right?
 
Peter Loew
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes it is:

 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What container are you using?
 
Peter Loew
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have tested this on both standalone OC4J and Oracle 9i Application Server. Same results.
 
Peter Loew
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well look what I found here:

https://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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to the spec, contextInitialized should be fired before any servlet initializations.

 
Peter Loew
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

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

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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad you got it working.
Thanks for posting back.
 
Neeraj Dheer
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that was very interesting...thanks for letting us know...
 
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic