aspose file tools*
The moose likes Servlets and the fly likes Servlet init() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet init()" Watch "Servlet init()" New topic
Author

Servlet init()

yogen joshi
Greenhorn

Joined: May 02, 2003
Posts: 21
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

Joined: Jan 08, 2003
Posts: 134
Only one time..


"Failure is not when you fall down; its only when you fail to get up again."
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Sripathi Krishnamurthy
Ranch Hand

Joined: Mar 07, 2005
Posts: 232
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

Joined: Sep 12, 2005
Posts: 73
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.


Chop your own wood, and it will warm you twice. - Henry Ford
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
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.


SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

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.
 
 
subject: Servlet init()