Creating more than one servlet objects

In this post, you will learn:

  • can more than one servlet objects be created by the server in a web application?
  • If yes, then how can multiple objects be created?
  • If not, then why?

Can more than one servlet objects be created by the server?

Most of the people reply no. They assume that only one object of a servlet can be created by the server in an application. This is the default behavior which can be overridden i.e. we can ask the server to create as many objects of a servlet as we wish.

Why more than one servlet objects are required?

Load sharing is the primary reason for creating multiple objects of a servlet. If large number of requests are expected for a servlet then mulitple objects need to be created. Load sharing is required in time bound acitivities such as flash sale of mobiles on flipkart, reservation of “Tatkal” tickets on IRCTC site etc. Thousand or may be lacs of users send requests at a specific time for the same task i.e for the same servlet. Imagine what will be the scenario if a single object is shared by lacs of threads!

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 index.html :

Object s1 of Simple Servlet <br/>
Object s2 of Simple Servlet 

Second the SimpleServlet, Two objects of it will be created by the server:

package com.techmentro.learningpad;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class SimpleServlet 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()
	{
		//Each time a servlet object is created, its init() method is invoked hence we are displaying a message
		//from it on the server console, to find out how many objects of the servlet are created.
		System.out.println(getServletConfig().getServletName()+" object of SimpleServlet is created.");
		
		
	}
	public void doGet(
			HttpServletRequest request, 
			HttpServletResponse response)
			throws ServletException, IOException {
		
		PrintWriter out=response.getWriter();
		out.println("<b> This request is processed by "+ getServletConfig().getServletName()+" object of SimpleServlet.</b><br/>");
		out.close();
		
		
	}
		

}

Third, the web.xml. Just check that both the <servlet> elements have same servlet class.

<?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">
  <!-- To get two objects created for SimpleServlet by the server, Two servlet entries are made for it. -->
  <servlet>
    <servlet-name>s1</servlet-name>
    <servlet-class>com.techmentro.learningpad.SimpleServlet</servlet-class>
    </servlet>
  <servlet-mapping>
    <servlet-name>s1</servlet-name>
    <url-pattern>/simpleServlet_instance1</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>s2</servlet-name>
    <servlet-class>com.techmentro.learningpad.SimpleServlet</servlet-class>
    </servlet>
  <servlet-mapping>
    <servlet-name>s2</servlet-name>
    <url-pattern>/simpleServlet_instance2</url-pattern>
  </servlet-mapping>
</web-app>

Output screen images of the practical example:

The first screen contains the home page and empty server console in the background.

multiObject_screen1

In the second screen the response page represents that the request is processed by s1 object of SimpleServlet and the console message represnts that s1 object of SimpleServlet is created. This output is the generated when first link of the home page is clicked by the user. Notice the URL in the address bar of the browser in the image.

multiObject_screen2

In the third screen the response page represents that the request is processed by s2 object of SimpleServlet and the console messages represnt that s1 & s2 two objects of SimpleServlet are created. This output is the generated when second link of the home page is clicked by the user. The URL in the address bar of the browser in the image, verifies this.

multiObject_screen3

Its time to recap the main concepts:

  1. By default, only one object is created for a Servlet by the server.
  2. We can ask the server to create as many objects of a servlet as we need.
  3. Multiple <servlet> elements need to used for the same servlet class in web.xml to get its mulitple objects created.
If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Leave a Reply

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