• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

load-on-startup tag Vs listener

 
udhaya kanagaraj
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai Saket Barve,
Thanks for ur reply

Regards,
udhaya
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic