• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Amandeep Singh
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry the complete question is




Which of the following statements is correct regarding the servlets that implement the SingleThreadModel?
 
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hope this helps.


Single Thread Model:



the correct answer is : 5
[ August 05, 2008: Message edited by: deepa raj ]
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Amruth Puppala
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's say 99% correct :-)

It is 100% correct if you substitute might create for will create in the last sentence.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic