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 load-on-startup  tag  Vs  listener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "load-on-startup  tag  Vs  listener" Watch "load-on-startup  tag  Vs  listener" New topic
Author

load-on-startup tag Vs listener

udhaya kanagaraj
Ranch Hand

Joined: Mar 31, 2005
Posts: 62
hi all,

Doubt
----

load-on-startup tag is used to execute code(such as database connection) in init method when server is loaded.according to my knowledge listener
also used to execute code when server is loaded. what is difference between
load-on startup used in xml and listener interface.

thanks in advance,
udhaya
Saket Barve
Ranch Hand

Joined: Dec 19, 2002
Posts: 229
Isn't the basic purpose of listener to notify of a specified event taking place? Listener's task would come into picture after the concerned event has occured.
udhaya kanagaraj
Ranch Hand

Joined: Mar 31, 2005
Posts: 62
hai Saket Barve,
Thanks for ur reply

Regards,
udhaya
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Load on startup, as the name implies will force a servlet to load when the app is started. This is useful if the servlet has some heavy weight tasks to perform in order to initialize itself such as reading config files, getting init information from a database etc..

Before servlet spec 2.3 this was also the only way to force initialization code required by the whole app to be run.

There are a few problems with this and context listeners solve them.
First and foremost, this runs against the principal of a servlet being an object that waits for requests and responds to them.

Second, according to the spec, a container can unload a servlet to conserve resources if necessary. It would then reload it again if a request was made for it. If this were to happen to your init servlet, your initialization setup and teardown code could be run at random times during your app's lifecycle.

The context listener's contextInitialized method gets called once, when the app loads, before any servlets are loaded. Likewise the contextDestroyed method gets called once, after all of the servlets have been unloaded.
These methods cleaner and more self evident.

Load-on-startup is still useful but should only be used to fire initialization code for a particular servlet, not the app as a whole.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by udhaya kp:
hi all,

Doubt
----

load-on-startup tag is used to execute code(such as database connection) in init method when server is loaded.according to my knowledge listener
also used to execute code when server is loaded. what is difference between
load-on startup used in xml and listener interface.

thanks in advance,
udhaya



Listener interface can be implemented by the programmer..and according to events ..like context created and context destroyed there callback functions in the Listener interface would be called.we can do some processing before context is created or when the context is destroyed.

<Load-On-Startup/> tag is there in the web.xml where you can specify the servlets which are suppose to get initialized irrespective of requests.when the context/application gets up then these servets's init is called.

cheers!


Rahul Bhattacharjee
LinkedIn - Blog
 
GeeCON Prague 2014
 
subject: load-on-startup tag Vs listener