File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Servlet Multiple instances? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Servlet Multiple instances?" Watch "Servlet Multiple instances?" New topic

Servlet Multiple instances?

Kris Reid
Ranch Hand

Joined: Jan 05, 2005
Posts: 247
I have been using the Ethuware software for mock exams.
It had a question on SingleThreadModel that I got wrong because they say the container may create multiple instances of a servelet. Is this true?

Here is what they say:

SingleThreadModel interface is just a tag interface that directs the servlet container to serialize the requests to this servlet instance i.e. multiple simultaneous requests to this servlet will be processed one by one instead of gettting processed simultaneously by using multiple threads. Therefore, multiple threads will not run over the methods of this servlet instance simultaneously.

BUT, to efficiently service requests, the container may create multiple instances of this servlet class. It may then process multiple requests using different instances of the servlet class simultaneously. Obviously, some requests will be serviced by one instance and some by other instance.
In this question, there may be multiple instances of the servlet class. So, requests will be serviced by different instances. Therefore, the remote addresses for the different requests will be added to StringBuffers of different instances.
Anand Wadhwani
Ranch Hand

Joined: Mar 21, 2005
Posts: 151
Yes, all the explaination given is very well correct.

Single Thread Model
Container uses a single thread to service all client requests with the help of a servlet instance (there could be multiple for reducing response time or container can reuse existing stale servlet instances to optimized use of memory)

Multiple Thread Model (default)
Container uses single instance of servlet, but multiple threads' run() method using the same servlet instance, each thread servicing a request concurrently using common shared servlet instance.

Still confused? API description may be helpful:

Or try reading out specs.

Hope this helps.

SCWCD 1.4<br />---------------------<br />Ability is what you're capable of. <br />Motivation determines what you do. <br />Attitude determines how well you do it.<br />---------------------
I agree. Here's the link:
subject: Servlet Multiple instances?
It's not a secret anymore!