Wednesday, June 18, 2008

What's Servlet Preinitialization & Lazy Initialization?

Preinitialization of Servlets

As the name suggests, Servlet Preinitialization means having a servlet initialized beforehand i.e., having a servlet initialized before being requested to service an incoming request.

This is done by the Application Server where the Web Application (having the servlets) is deployed. Most of the popular application servers use an element called 'load-on-startup' in their Deployment Descriptor file named web.xml to specify the servlets which are supposed to be pre-initialized at the time of the server startup itself. Read more in this article - Using 'load-on-start' to Pre-initialize Servlets >>

Lazy Initialization or Lazy Loading of Servlets

This process causes a servlet to be initialized (or loaded by the Servlet Engine) only after an incoming request call it for the first time. These servlets are obviously those which are not mentioned in the web.xml file for pre-initialization. All the servlets which are responsible to service the routine type and frequest tasks are generally pre-initialized and those which serve only some rare specific purpose are normally preferred to be initialized only when they are called for the first time.

Advantages and Disadvantages of the two

Quite obvious to figure out. Pre-initialization will cause a servlet to perform little faster even for the first incoming request as it's already loaded and ready to serve the request. Other the other hand, lazy initialization will cause the first request to be served with a slight delay as the Servlet Engine will start the initialization of the servlet only after the first request has already been received. The disadvantage of pre-initilization is that it makes the server startup process little longer as the server needs to initialize all the servlets mentioned in the deployment descriptor during its startup. Another potential disadvantage of pre-initialization is that if a rarely used servlet is pre-initialized then it may waste the precious memory for a long time until the first request for this servlet comes.


1 comment:

Pramma said...

Nice one. thanks