Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

load a servlet after startup

 
sree visu
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • 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
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • 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?
 
Paul Michael
Ranch Hand
Posts: 697
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any way to call a servlet only once after the application starts


Ever considered using Listeners?
 
Milton Ochoa
Ranch Hand
Posts: 336
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64858
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13062
6
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64858
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, please read my response again. Solve the real problem, don't try to find a way around it.
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • 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.
 
Duc Vo
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic