Hi,
Sometime we need some services to be initialized when application gets started, Few application servers like weblogic provides handy feature where you can add startup classes when server gets started. There is a very nice and easy way where we can add bootstrap classes when application gets started. These classes we can use to init some services like logger,email agents, service agents, db factories etc. The below example will show how to do that using a simple servlet.
Step 1: write a simple servlet
public class StartupService extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
/**
*
*/
public void init(ServletConfig config) throws ServletException {
/*
ADD Your logic here…
*/
System.out.println(“Startup class initialized successfully”);
}
}
Step2: Add the below mapping entry in web.xml
<servlet>
<servlet-name>StartupService</servlet-name>
<servlet-class>com.example.StartupService</servlet-class>
<!– Container/server will start the servlet when app gets start –>
<load-on-startup>1</load-on-startup>
</servlet>
Step 3: Start the server. you might will see “Starup class initialized successfully”.
—
Thanks
R Vashi
Can you elaborate more how can add db factories which can be available through out the life of application.
Hi Ashish,
Thanks for the query.
A DbFactory defines all the interface methods that any client needs to implement to supply the instances of their XXXXDB interface implementations. This class is the single source for object creation. Application clients can set/get their particular choice of DbFactory implementation through the static methods of the DBManager class.
or in other way you can also define a static Data Source object which points to one specific database using JNDI name.
but all these rules require a static/singleton object which can make sure that it is global having single reference to the entire scope of the application. and in the startup servlets we have to just initialize those objects only once so that could be in the position to service the client request.
Hope this helps, please let me know if you have any doubts.
—
thanks
admin