aspose file tools*
The moose likes Servlets and the fly likes How to write a bootstrap code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to write a bootstrap code" Watch "How to write a bootstrap code" New topic
Author

How to write a bootstrap code

rames tirrupur
Greenhorn

Joined: Jul 25, 2006
Posts: 13
Hi All,

I want to load the values during server startup. How to achieve this.
I plan to have a bootstrap process. How to write a bootstrap program and it should be invoked.

Any suggestions on this.

Thank you,
Ramesh
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

This sounds like a weird proposition but :
Declare a servlet whose load-at-startup value is positive, and do your loading stuff in the init() method of that servlet.


[My Blog]
All roads lead to JavaRanch
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Satou kurinosuke:

Declare a servlet whose load-at-startup value is positive, and do your loading stuff in the init() method of that servlet.


Prior to Servlet Spec 2.3 this was really your only option.
Now, the best practice is to use a ContextListener for this.
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContextListener.html

It's cleaner, more sound, and it lets servlets be servlets.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

I gave it a glimpse of thought but you still have to load a servlet at startup, haven't you ? Otherwise the listener won't be notified.
(I agree that servlets should be servlets)
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

The contextInitialized method of the ContextListener is called when the context fired up. This happens before any servlets are loaded.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

You're right
Thanks for pointing it out.
Janakiram Gattupalli
Greenhorn

Joined: Jul 30, 2006
Posts: 8
We can do it by implementing ServletContextListener.In ServletContextListener interface we can use contextInitialized method.In this method get the reference with ServletContext.

Here I am demonstrating

public class MyApp implements ServletContextListener(ServletContextEvent sce)
{
ServletContext application=sce.getServletContext();
// initialize variables
application.setAttribute("var",key);
//other methods
}
 
wood burning stoves
 
subject: How to write a bootstrap code