Bootstrap servlet or Startup servlet

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

2 thoughts on “Bootstrap servlet or Startup servlet

    • 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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s