• 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 init()

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
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.
Yogen
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only one time..
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Only one time..



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.
 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



Bear,
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".
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by yogen joshi:
Hi,
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.
Yogen


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.
 
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Servlet spec, SRV.2.2


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.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The key phrase from the spec is

the servlet
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic