aspose file tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Number of Instances Per Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Reply Bookmark "Number of Instances Per Servlet" Watch "Number of Instances Per Servlet" New topic
Author

Number of Instances Per Servlet

stan lee
Greenhorn

Joined: Apr 08, 2003
Posts: 21
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

Joined: Sep 23, 2000
Posts: 2912
In this case, it is not possible. Multiple instances are created only if the servlet implements SingleThreadModel.


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Sai Prasad
Ranch Hand

Joined: Feb 25, 2002
Posts: 560
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

Joined: Mar 26, 2003
Posts: 194

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

Joined: Apr 08, 2003
Posts: 21
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

Joined: Mar 26, 2003
Posts: 194
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Number of Instances Per Servlet
 
Similar Threads
question on servlet mapping !!!
Panel vs Frame
classes
Read a 2d array into a method?
synchronized keyword usage