aspose file tools
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
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Servlets
Reply 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: 14672
    
  11

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: 4967

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 ]

Author of Hibernate Made Easy, What is WebSphere???, JSF 2.0 Made Easy and the SCJA Certification Guides
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to load a class on app server startup
 
Similar Threads
instances of compnents within application
Servlets
servlet calling applet
how can i integrat JSP, java class and coldfusion`please help me
No Factories configured for this Application