• 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

Servlet instances and its init method

 
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've a servlet that made use of its init method to read a config file and set the values into the servletcontext. Whenever a new HTTP request comes in, will the container creates just 1 instance of the servlets, or many instances could be created?

If 1..* instances of the servlet could be created, does it means that the init method will be called each time an instance is created?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only one instance is created. I'm surprised that this isn't covered by the SCWCD exam, which -judging by your signature- you have taken.
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is your servlet implementing SingleThreadModel?
 
Chengwei Lee
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ulf Dittmer:
Only one instance is created. I'm surprised that this isn't covered by the SCWCD exam, which -judging by your signature- you have taken.




In the case where a servlet was deployed as part of an application marked in the deployment descriptor as distributable, a container may have only one instance per servlet declaration per virtual machine (VM).



I was reading the specifications and found the use of the word may, hence the query. Wouldn't this means its up to the servlet container?
 
Chengwei Lee
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Amol Nayak:
Is your servlet implementing SingleThreadModel?



Nope, not using that.
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only one instance is created.

If you implement SingleThreadModel interface the server will create multiple instances of servlet.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Amit Kumargupta:
If you implement SingleThreadModel interface the server will create multiple instances of servlet.



This is not true. Firstly the question is about Servlet instances and the init() method, not SingleThreadModel.

But since it was brought up, this is from the API:

If a servlet implements this interface, you are guaranteed that no two threads will execute concurrently in the servlet's service method. 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.


(bold added by Dave)
Neither of these mention a new instance for each request, although it is a less efficient version of the second option.
[ August 03, 2007: Message edited by: David O'Meara ]
 
Chengwei Lee
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by David O'Meara:

(bold added by Dave)
Neither of these mention a new instance for each request, although it is a less efficient version of the second option.

[ August 03, 2007: Message edited by: David O'Meara ]



Would it then be safe to assume that it's 1 instance per servlet per JVM, or it's vendor-specific? You're right that it's not mentioned that each request will get a new instance, but I can't find seems to locate where it states that it is guaranteed to be exactly 1 instance per servlet per JVM.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is one Servlet instance per servlet mapping, since a Servlet class is able to be mapped several times in web.xml
 
Chengwei Lee
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by David O'Meara:
It is one Servlet instance per servlet mapping, since a Servlet class is able to be mapped several times in web.xml



Thanks.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chengwei Lee:
I've a servlet that made use of its init method to read a config file and set the values into the servletcontext. Whenever a new HTTP request comes in, will the container creates just 1 instance of the servlets, or many instances could be created?

If 1..* instances of the servlet could be created, does it means that the init method will be called each time an instance is created?





Hello Mr.Lee,,

First thing to note is ,only one instance of servlet will serve all the request's ,secondly multiple instances are created of a servlet when you implement single thread model,Third thing is init method is called only once for every instance of a servlet,
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are implementing SingleThreadModel, then obviously, for superior performance you'd want the servlet container to create multiple instances of a SingleThreadModel type servlet should there be many requests received in quick succession. Whether or not a servlet container does that depends completely on the implementation. So it is not guaranteed that if you implement SingleThreadModel, the container will create multiple instances for that servlet.

Regards
Prem Kashyap
SCJP 1.4 : 88%
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Prem Kashyap:
If you are implementing SingleThreadModel, then obviously, for superior performance you'd want the servlet container to create multiple instances of a SingleThreadModel type servlet should there be many requests received in quick succession. Whether or not a servlet container does that depends completely on the implementation. So it is not guaranteed that if you implement SingleThreadModel, the container will create multiple instances for that servlet.

Regards
Prem Kashyap
SCJP 1.4 : 88%



Prem,
We appreciate your participation in these forums.
Before doing so, however, please take the time to read the entire thread.
In this case the original poster has already stated that he is not using SingleThreadModel. Adding any discussion of SingleThreadModel after that only makes the thread longer and more confusing.

Thanks,
-Ben
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Santhosh Reddy:




Hello Mr.Lee,,

First thing to note is ,only one instance of servlet will serve all the request's ,secondly multiple instances are created of a servlet when you implement single thread model,Third thing is init method is called only once for every instance of a servlet,



Santhosh Reddy,
We appreciate your taking the time to answer questions in this forum.
Before doing so, in the future, please read the entire thread.
In this case the original poster's question has already been answered.
Answering again, especially if you're not elaborating on the first answer, only makes the thread longer and more confusing.

Thanks,
-Ben
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chengwei Lee:


I was reading the specifications and found the use of the word may, hence the query. Wouldn't this means its up to the servlet container?



The init method is called when the servlet is put into service.
The servlet spec allows containers to take a servlet out of service if it wants to (with the idea that a container might want to take a rarely used servlet out of service to conserve resources). In this case, the init method would be called again when and if the servlet were put back into service.

This is one of the reasons that we push the use of context listeners for application initialization code over the older method of putting this code in the init method of a servlet and then using load-on-startup to push this code to be run when the app starts.
 
Prem Kashyap
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
=================================================
Prem,
We appreciate your participation in these forums.
Before doing so, however, please take the time to read the entire thread.
In this case the original poster has already stated that he is not using SingleThreadModel. Adding any discussion of SingleThreadModel after that only makes the thread longer and more confusing.

Thanks,
-Ben
=====================================================

I will take this into consideration from next time.

Thanks
Prem Kashyap
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic