Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet init()

 
yogen joshi
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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
 
Shankar Narayana
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only one time..
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Sripathi Krishnamurthy
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • 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".
 
Kartik Patel
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • 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.
 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic