Originally posted by Bear Bibeault:
Quite incorrect. Each servlet put into service via a <servlet> declaration will have its init method called with the servlet config containing any init params for that declaration.
I think he is asking whether the init will be called per servlet instance or not.
if there are 10 servlet instances of servlet A, then init will be called 10 times.
"init()can only be called once per servlet instance".
Originally posted by yogen joshi:
Normally only 1 instance of a servlet is created, but if we configure the web server to have multiple instance say 10 instances of a servlet A, then in that situation, how many times will the init() method be called?
1 or 10 times?
Thanks in advance for your answer.
I think the init will get call for 10 times as servletconfig is individual for each servlet and u have 10 instances of the servlet and if all have implemented SingleThreadModel than each time request comes a new instance will be initialised if old one is processing another request so if u have 10 single thread servlets and u have 10 request to process simultaneously than 10 time init method will get called. this is not true in the case of multi threaded servlets.
For a servlet not hosted in a distributed environment (the default), the servlet
container must use only one instance per servlet declaration. However, for a
implementing the SingleThreadModel interface, the servlet container serv-let may
instantiate multiple instances to handle a heavy request load and serialize requests
to a particular instance.
If you will use SingleThreadModel the number of instances depends on container.
container must use only one instance per servlet declaration.
Each servlet declaration gets its own instance of a servlet regardless of whether an instance of that same servlet has already been created as a result of a different servlet declaration.