wood burning stoves 2.0*
The moose likes Servlets and the fly likes how to load a class on app server startup Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to load a class on app server startup" Watch "how to load a class on app server startup" New topic
Author

how to load a class on app server startup

shashiD kanth
Greenhorn

Joined: Aug 18, 2006
Posts: 6
Hi all,
I have a java class, not servlet. I want to load this class as soon as servlet container starts. In that java class I have written some code in static block, which is executed as soon as class is loaded into JVM.
Thanks,
Shashi.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You could just implement a ServletContextListener that calls your class in its contextInitialized method.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
shashiD kanth
Greenhorn

Joined: Aug 18, 2006
Posts: 6
Can you please give some example code on this. Your help is appreciated.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

1. Create a class implementing javax.serlvet.ServletContextListener
2. Implement both contextDestroyed and contextInitialized
3. Instanciate your class in contextInitialized
4. Declare the listener in web.xml using the <listener> tag


[My Blog]
All roads lead to JavaRanch
shashiD kanth
Greenhorn

Joined: Aug 18, 2006
Posts: 6
Thanks!!! it worked for me
subbarao N.V
Greenhorn

Joined: Aug 21, 2006
Posts: 13
hi

Suppose your class is student,you create a subdirectory as part of bea directory in the weblogic and place the student.class.Before starting your weblogic set the class path to student.class and start the server.


This is the one way


It is performance wise good.
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Just make sure you NEED that class loaded at startup.

Lazy Loading should be the rule, not the exception. I once workd on a massive piece of iron that took 30 minutes to boot, just because the developers decided every EJB had to be in memory before the application should handle any client requests.

Lazy Loading Rocks. Everyone, BE LAZY!!!
[ August 27, 2006: Message edited by: Bear Bibeault ]
 
Consider Paul's rocket mass heater.
 
subject: how to load a class on app server startup
 
Similar Threads
Servlets
instances of compnents within application
how can i integrat JSP, java class and coldfusion`please help me
servlet calling applet
No Factories configured for this Application