• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Number of Instances Per Servlet

 
stan lee
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a question regarding the number of instances per servlet.
Consider this code:
public class TestServlet extends HttpServlet {
public void doPost(req,res) {
// Blah Blah
TestServlet testServlet[] = new TestServlet[50];
for(int i=0;i<testServlet.length;i++)
out.println(testServlet[i]);
//Blah Blah
}
}
In the above code, 50 instances of TestServlet are created.How is this possible as specification says there is only one instance existing.
Any Thougths?
Stan Lee
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3817
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this case, it is not possible. Multiple instances are created only if the servlet implements SingleThreadModel.
 
Sai Prasad
Ranch Hand
Posts: 560
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Specification is written for Servlet Container developers. It doesn't prevent any servlet implementation developer to create 50 or more servlet instances.
It is up to the container to decide the number of servlet instances based on your implementation of servlet. If you implement SingleThreadModel, the container may block simultaneous requests or create multiple instances of servlet.
 
S. Ganapathy
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You are able to create an array of TestServlet which can hold 50 objects. It is an empty array. There is no statement which creates servlet instances. You may call default constructor, but init() method is the one which is called by the servlet container to create the servlet instance. There is lot of difference between an object, and a servlet instance. (Servlet instance is an object, but all objects are not servlet instances). Container may create multiple instances depending on the load for this servlet, but it is not transparent to the user.
 
stan lee
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Venkat! Got it, Here the code is creating 50 Servlet instances which is of no use ..i mean it does not have the run time properties of a Servlet, where as the container creates only one instances with all the required properties.
Regards!
Stan Lee
 
S. Ganapathy
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi stan lee,
Depending on the load, container creates number of servlet instances, developer doesnt have any control over that. But most of the cases, it maintains single instance.
GVRao
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic