• 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

load a servlet after startup

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear ranchers,
It is known that the servlet initialisation is the last thing that is done when an application starts after listener,context and filter initialisation.However I have a servlet that accesses the database which has to be started immediately after startup.The <load-on-statuup> doesn't help( as the Context object(JNDI) doesn't get initialised at that point).Is there any way to call a servlet only once after the application starts
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sree visu wrote:However I have a servlet that accesses the database which has to be started immediately after startup.



What do you mean by this? Your database not started when the application starts up? Can you please elaborate little?
 
Ranch Hand
Posts: 697
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Is there any way to call a servlet only once after the application starts



Ever considered using Listeners?
 
Ranch Hand
Posts: 336
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Difininitive you should use a listener,

I supose that you want is get a Connection to a DB.

 
sree visu
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your replies and here are my answers to the queries but did not solve the problem

To:
Vijitha Kumara:
I perform a JNDI lookup which uses a context object for the lookup.But when i started the application it threw an illegal state exception (after the null pointer exception) saying that the context object does not exist

To
Paul Michael:

Yes!But listeners gets instanitated first all before everything.I called the method which accesses the database in a constructor inside the servlet so that all the objects gets initialized before the application starts.But i just instanitated at the last step ie I used only <load-on-startup> in my web.xml file which is the servlet instantiation which happens after listener,context and filter instantiation in the order which i have specified.But even at that stage the Context object did not exist

To
Milton Ochoa:
I am really sorry that as i mentioned before the listener will not help in my case I suppose.Please be brief on how to tackle this situation.Thanks in advance.

Once again thanks to everyone for your reply.
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sree visu wrote:But when i started the application it threw an illegal state exception (after the null pointer exception) saying that the context object does not exist

You need to find out why. You should never have to put in artificial delays. That's just a band-aid that is likely to come off at the most inopportune moments.
 
Milton Ochoa
Ranch Hand
Posts: 336
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi again, the tag <load-on-startup> I mean, that load the servlets within startup of the web application, i really dont know How its work, but I think that is not the solution.

I think that you could get a CONTEXT (the object that use the conection to the db) using the listener: ServletContextListerner, and using the tag on web.xml <listener>

I dont know if you get my idea.

Sorry my bad english.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

illegal state exception (after the null pointer exception) saying that the context object does not exist




Good programming practice would be to solve the NPE first.

Bill
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, please read my response again. Solve the real problem, don't try to find a way around it.
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can't agree more with Bear and others. Your problem lies somewhere else. ServletContextListener should solve your problem but only if you correctly use that feature.
 
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure if anyone picked this up before.

sree visu wrote:
Yes!But listeners gets instanitated first all before everything.I called the method which accesses the database in a constructor inside the servlet so that all the objects gets initialized before the application starts.But i just instanitated at the last step ie I used only <load-on-startup> in my web.xml file which is the servlet instantiation which happens after listener,context and filter instantiation in the order which i have specified.But even at that stage the Context object did not exist



Because, you use the constructor. You should never use contructor to initialise your servlet object and/or any listener object. Move your code in the constructor to the overrided init method of the servlet. It should work OK.

Hope it help,

Duc
 
Space seems cool in the movies, but once you get out there, it is super boring. Now for a fascinating tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic