Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

when init() is being called?  RSS feed

 
Sean Hetfield
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wonder if the init() method will be called only once at the load time or each time that servelt being called and request being passed to it, init() method being called as well?

Thanks

Sean
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66203
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only once upon initial load.
 
Chetan Raju
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding to that, is init() method called for all instances of servlets created by the servlet container ?
 
Lanka Prasad
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>> Adding to that, is init() method called for all instances of servlets created by the servlet container ?

Only one instance is there and container creates a new thread of the servlet per request. init called only once.

-Lanka
 
Chetan Raju
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well that depends on the servlet container... it can create more than one instance of servlet to service the requests right ?
 
Lanka Prasad
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There may be (not sure) such servlet containers. I have tested with TomCat with an overriden init method it was called only once.

but javax.servlet.http.HttpServlet API says:
1. The servlet is constructed, then initialized with the init method.
2. Any calls from clients to the service method are handled.
3. The servlet is taken out of service, then destroyed with the destroy method, then garbage collected and finalized.

not mentioned about init under 2.
When we are studying about servlet's life cycle we must be sure about this.
Do you think servlet's life cycle is different from container to container ?

-Lanka
 
Chetan Raju
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not have a doubt about the life cycle of a servlet and it is the same in all containers.. what I want to ask is that the container can create multiple instances of the servlet. In this case does the init method be called for all such instances of the servlet ?
 
Lanka Prasad
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May be you are right Raju.
If we can experiment with it we can see. But I am still not expert to do it with containers.

Theoriticaly, init() is being called per instance.
Let's wait for an expert to help us on this.
-Lanka
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think if the servlet implements SingleThreadModel (which is deprecated and not recommended), then the container will create as many servlet instances as is required.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each instance of a servlet will have init called before any request is processed - that is the servlet container contract.
In addition to the SingleThreadModel (do NOT use it) there are other reasons that multiple instances of the same servlet class will be created.
Look at the servlet API - the same class can be used multiple times to declare servlets in web.xml as long as the servlet-name entries are different.

I find it good practice to have init methods do a System.out.println just to verify when init is called.

Bill
 
Majid Al-Fifi
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought the sepc says a Servlet is guaranteed to have only one instance but as William Brogden mentioned above we can have it defined several times in web.xml and I have tested that.

Are there issues like syncronization with this implementation?
What is the reason to allow it?

Thanks,
Majid
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are there issues like syncronization with this implementation?
What is the reason to allow it?


Synchronization: that depends on whether the instances are in the same web application-same class-loader and use static variables. With good practice there should be no problem.

Reason: It just falls out of the way servlets are loaded and configured, but note that a general purpose servlet could have different instances that are configured (and named) differently.

Bill
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!