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.
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.