• 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

when init() is being called?

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
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 once upon initial load.
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adding to that, is init() method called for all instances of servlets created by the servlet container ?
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
His brain is the size of a cherry pit! About the size of this ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic