Creating servlet objects at the time of deployment

In this post, you will learn:

  • can servlet objects be created at the time of deployment?
  • If yes, then how can they be get created?
  • Why are servlet objects created at the time of deployment?

Can servlet objects be created at the time of deployment?

In servlet, it is taught that a servlet object is created by the server when first request is received for it. This is not the complete truth, actually it is the default behavior of the server which can be overridden i.e. Sevlets objects can be created by the server at the time of application deployment.

What is the need of creating servlet objects at the time of deployment?

Reducing the response time of the first request is the primary reason for creating servlet objects at the time of deployment. If a servlet requires a lengthy initialization such loading configuration files, creating a large set of objects etc then its first request will take more time than the subsequent requests. If object of such a servlet is created at the time of deployment then this time lag can be eliminated.

Now you will be wondering in which case such a lengthy initialization is required. The controller component of MVC is the perfect example of this. The controller in a MVC application act as single entry point into the application i.e. it receives all the requests and controls their processing. For this it need the configuration of all the components of the application and need to instantiate them before they are used. If such a controller is implemented through a servlet as is done in Spring MVC then it should be instantiated at the time of deployment.

How more than one servlet objects can be created by the server?

We can ask the server to create multiple objects of a servlet by adding multiple <servlet> entry in web.xml for the same servlet class.

Practical example of mulitple objects of servlet:

First the ServletOne. I haven’t defined doGet() or doPost() method to keep the example simple. Only the object creation process is to be understood through this example.

package com.techmentro.learningpad;

import javax.servlet.http.*;

public class ServletOne extends HttpServlet
{
	
	//A helper method provided by GenericServlet class to define initialization of servlet by the programmers.
	//It is invoked by init(ServletConfig config) life cycle method which is defined in GenericServlet.
	public void init()
	{
		//When the object of this servlet is created, following message will be displayed on the console.
		System.out.println(getServletConfig().getServletName()+" object of ServletOne is created.");
		
		
	}
	
}


First the ServletTwo. Similar to ServletOne, it doesn’t have doGet() or doPost() method.

package com.techmentro.learningpad;


import javax.servlet.http.*;

public class ServletTwo extends HttpServlet
{
	
	//A helper method provided by GenericServlet class to define initialization of servlet by the programmers.
	//It is invoked by init(ServletConfig config) life cycle method which is defined in GenericServlet.
	public void init()
	{
		//When the object of this servlet is created, following message will be displayed on the console.
		System.out.println(getServletConfig().getServletName()+" object of ServletTwo is created.");
		
		
	}
	

}


Third, the web.xml. Just check that both the <servlet> elements have <load-on-startup> sub elment. The value of <load-on-startup> of ServletOne is higher than the value of ServletTwo so that ServletTwo object is created before ServletOne. Further note that we have not provided <servlet-mapping> elements for them because we don’t intend to send any requests for them in this example.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
   
<!-- We want these servlets to be instantiated at the time of deployment in the following order: ServletTwo,ServletOne.  -->
  <servlet>
    <servlet-name>s1</servlet-name>
    <servlet-class>com.techmentro.learningpad.ServletOne</servlet-class>
    <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet>
    <servlet-name>s2</servlet-name>
    <servlet-class>com.techmentro.learningpad.ServletTwo</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
</web-app>

Output screen images of the practical example:

The image contains the server console output which was generated at the time of deployment. It contains statements which we printed from init() methods of ServletOne & ServletTwo. The output clearly demonstrate the order of object creation.

loadOnStartup_screen

Its time to recap the main concepts:

  1. By default, A servlet object is created by the server only when the first request is received for it.
  2. By adding <load-on-startup> sub element of servlet we can get its object created at the time of deployment.
  3. Servlet objects are created by the server at the time of deployment in the ascending order of their <load-on-startup> value.

Example Source Code

If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

One thought on “Creating servlet objects at the time of deployment

Leave a Reply

Your email address will not be published. Required fields are marked *