• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about the init() method.

 
Gaurav Sahni
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the API if a servlet does not implement SingleThreadModel then only one instance of it is created and mutiple threads share this instance. Whenever the container loads the servlet, it initializes it, calling the init() method on that instance.

However, in cases of high load, the container may also instantiate multiple instances of the servlet. In this case, will init() be called on each of the instances ?
 
Gaurav Sahni
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nobody to help??? :roll:

Let me make the question a bit simple.

Suppose i have written a servlet which is handling the client request and doing some database manipulations based on it. Obviously, i would use the init method to open the database connection, and perform other initializations(if any).

Now, the question is suppose the no. of requests are v.high, and one instance of the servlet might not be able to serve all the threads. therefore, the container might decide to create multiple instances of the same servlet. In such a case, would the init() method be called on each instance of the servlet ?

Regards,
Gaurav.
 
Colin Fletcher
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have answered your own question.



Whenever the container loads the servlet, it initializes it, calling the init() method on that instance.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic