This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Servlets and the fly likes load a servlet after startup Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "load a servlet after startup" Watch "load a servlet after startup" New topic
Author

load a servlet after startup

sree visu
Ranch Hand

Joined: Jul 05, 2007
Posts: 133
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


Regards<br />Sree Visu<br />SCJP 5.0, SCWCD 5.0, SCJD(Preparing B&S 2.2.2)
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3833

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?


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Paul Michael
Ranch Hand

Joined: Jul 02, 2001
Posts: 697
Is there any way to call a servlet only once after the application starts


Ever considered using Listeners?


SCJP 1.2 (89%), SCWCD 1.3 (94%), IBM 486 (90%), SCJA Beta (96%), SCEA (91% / 77%), SCEA 5 P1 (77%), SCBCD 5 (85%)
Milton Ochoa
Ranch Hand

Joined: Oct 23, 2007
Posts: 336

Difininitive you should use a listener,

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

sree visu
Ranch Hand

Joined: Jul 05, 2007
Posts: 133
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

Joined: Jan 10, 2002
Posts: 61315
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Milton Ochoa
Ranch Hand

Joined: Oct 23, 2007
Posts: 336

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

Joined: Mar 22, 2000
Posts: 12792
    
    5
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

Joined: Jan 10, 2002
Posts: 61315
    
  66

Yes, please read my response again. Solve the real problem, don't try to find a way around it.
Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231
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.


thanks
Harvinder
Duc Vo
Ranch Hand

Joined: Nov 20, 2008
Posts: 254
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


“Everything should be as simple as it is, but not simpler.” Albert Einstein
 
GeeCON Prague 2014
 
subject: load a servlet after startup