wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes   Which of the following statements is correct regarding the servlets that implement 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 "  Which of the following statements is correct regarding the servlets that implement " Watch "  Which of the following statements is correct regarding the servlets that implement " New topic
Author

Which of the following statements is correct regarding the servlets that implement

Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
Select 1 correct option.

1. The container synchronizes access to all variables.


2. The container synchronizes access to session attributes.


3. Only one instance of the servlet is created.


4. One instance of the servlet per request is created.

5. For simultaneous requests, the container may create more than one instance of the servlet.



You answered WRONG. Correct options are highlighted.

The correct answer given is 5.

But according to me the correct answer is 4. Always one instance of the servlet is created and there can be number of requests to the one instance. But as in our case the servlet is implementing the SingleThreadModel. So only one is created per one instance. As the instance will always remain one for particular servlet in one JVM as always the case is.



Detailed Explanation

When a Servlet implements SingleThreadModel interface (This interface it has no methods), the servlet container makes sure that at a time only one thread is executing it's methods. If there are multiple simultaneous requests to the same servlet then it may create multiple instances of that servlet. So multiple thread can execute the methods but on different objects. So instance members are virtually thread safe.


This question is from Enthuware.

Please do advise.


SCJP 1.4, SCWCD 5, SCBCD 5, OCPJWSD 5,SCEA-1, Started Assignment Part 2
My blog- http://rkydesigns.blogspot.com
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
Sorry the complete question is




Which of the following statements is correct regarding the servlets that implement the SingleThreadModel?
Amruth Puppala
Ranch Hand

Joined: Jul 14, 2008
Posts: 295
Hi Amandeep Singh,

The correct option is 5

5. For simultaneous requests, the container may create more than one instance of the servlet.


Usually container creates a more then one instace per servlet and maintains the pool of instances. For each request one free instace is alloted. So container can create more then one instance of the servlet for simutaneous requests.

4. One instance of the servlet per request is created.


Option 4 may not be correct because container free to create any no of instaces but I won't create one instace per request becuase It has the capability to pool the instances.

So As per my knowledge option 4 is wrong.

Ranchers Correct me If I'm wrong..


SCJP 5 |SCWCD 5| Started thinking about Web Services ?
deepa raj
Ranch Hand

Joined: Jul 25, 2008
Posts: 124
Hope this helps.


Single Thread Model:



the correct answer is : 5
[ August 05, 2008: Message edited by: deepa raj ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41114
    
  45
Originally posted by Chintu sirivennela:
Usually container creates a more then one instace per servlet and maintains the pool of instances. For each request one free instace is alloted.

No. Usually the container creates a single instance, and all requests use that single instance from multiple threads.

As an aside, implementing STM does not make a servlet thread-safe; there are other sources of concurrency issues. So, simply never use it.


Ping & DNS - my free Android networking tools app
Amruth Puppala
Ranch Hand

Joined: Jul 14, 2008
Posts: 295
Hi Ulf Dittmer,

In the java API it was given
The servlet container can make this guarantee by synchronizing access to a single instance of the servlet, or by maintaining a pool of servlet instances and dispatching each new request to a free servlet.


What is the meaning of this statement. Does that bolded text mean Cantainer creates more then one instace per servltet and maintains pool?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41114
    
  45
Yes, that's what it means. But you can't know what a container will do with servlets that implement STM (not without looking at its source code or reading its documentation, anyway). Either of these is possible.
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
Ulf what i do understand from your replies is....

that when Servlet does not implement the STM, then the Servlet Container creates only single instance for a Servlet.

But when the Servlet implements a STM, it is not sure how the behaviour could be and multiple requests to the same Servlet will create multiple Servlet instances.

Please do adivse, Is this 100% correct....
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41114
    
  45
Let's say 99% correct :-)

It is 100% correct if you substitute might create for will create in the last sentence.
 
jQuery in Action, 2nd edition
 
subject: Which of the following statements is correct regarding the servlets that implement
 
Similar Threads
Servlet Multiple instances?
problem of thread safe
Urgent : Thread Safe
static class variables in SingleThreaded model
SingleThreadModel