• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

how many times init() method gets called

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

How many times init() method gets called when we implement the single thread model. I think it's only one time only, is it correct? . As I know for each request servlet container creates one servlet instance to serve the incoming request. Does init() method gets called for each servlet instance?
 
Ranch Hand
Posts: 96
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure if the servlet container creates a new instance of the servlet for each request. What happens is a single thread is executing at a given time; all other requests need to wait till that thread is finished executing. But there is only 1 instance of the servlet created. Hence, the question of init() method getting called multiple times doesnt arise. That method is called only once during the life cycle of the servlet.
 
ramesh turlapati
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Souvvik Basu, what you said is correct in you point of view. To implement single thread model there are two techniques.

1. Only one servlet instance and other request will wait in queue to get chance

2. For each request servlet container creates new servlet instance.

these techniques depends on third party vendors .
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
init() as well as destroy method is called only once during the life cycle of a servlet.

Only service method is called repeatedly
 
Ranch Hand
Posts: 174
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ramesh,

As you said For each Servlet instance, init() is called once.
If the Servlet Container is implemented to create multiple instances of servlet, then it will invoke init() method for each servlet instance.

BTW, SingleThreadModel is deprecated in Servlet 2.4 spec.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
from api


If a servlet implements this interface, you are guaranteed that no two threads will execute concurrently in the servlet's service method. The servlet container can make this guarantee by synchronizing access to a single instance of the servlet, or by maintaining a pool of servlet instances and dispatching each new request to a free servlet.



so as of tomcat 5.5, it wont create a new instance of servlet for each request even servlet implements SingleThreadModel. instead it synchorize access to service method.
 
Sheriff
Posts: 67753
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 single thread model was deprecated long long ago and should never be used.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic