Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to write a bootstrap code

 
rames tirrupur
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The contextInitialized method of the ContextListener is called when the context fired up. This happens before any servlets are loaded.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right
Thanks for pointing it out.
 
Janakiram Gattupalli
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic