• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Instantiating Servlet

 
vinora kumar
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
My doubt is when the Servlet getting instantiated.
�Container runs multiple threads to process multiple requests to a Single Servlet�
So their will be single instance for all the requests.
So am considering, Servlet is instantiated while starting the server.
But what�s the role of destroy?

So for me its sounds like a Servlet is instantiated before the first Request call my service method
Then when init() is called?
Getting confused?
When the Servlet Constructor called?
When the init() called?

The question might be silly, but the fact is am just learning about Servlet.
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinora I don't know about when a servlet is instantiated. It might be at the server startup but I am not sure. As far as init is concerned, it will always be executed before the servlet responds to the first request. If you declare load-on-startup on that servlet in the web.xml, then the init method will be called on server startup. The init method will only be called once on a servlet. The main use of the init method is to do any initialization work for the servlet eg. acquiring a database connection for use by the service method. After init the service method for that servlet will be called each time a request will be received. The servlet container will start a new thread for each request and call the service method. The servlet container will use the same instance of the servlet to process every request. The destroy method is called on the servlet instance when the server is shutdown or the web-application is undeployed. The main purpose of destroy method is to do any last minute processing required by the servlet like releasing any resources used by the servlet etc...
 
vinora kumar
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well that means always I have instance of Servlet in my container rite?
Suppose say for example no client requesting my particular Servlet,
Then what�s the use of maintaining the Servlet instance
What about the performance?

Any way I need to know who instantiating the Servlet?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"vinora",
Welcome to the ranch. You may not be aware of the ranch Naming Policy. Please read it carefully and change your name accordingly (you need to set both first and last names). Thank you.
 
vinora kumar
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
done
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that means always I have instance of Servlet in my container rite?

It's up to the servlet container when to instantiate the servlets. But note that the <load-at-startup> element in web.xml specifies that a servlet should be loaded when the web app is started, so most containers leave the instantiation of all other servlets until the moment when they are first accessed.

But I wouldn't worry about the impact of instantiating a single object; unless you're doing a ton of stuff in the init method, the impact should be small.
 
vinora kumar
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:

But I wouldn't worry about the impact of instantiating a single object; unless you're doing a ton of stuff in the init method, the impact should be small.



Well thanks for the info.
What if I have tons and tons of Servlets, but not even a single client interested on my Server?
[ December 18, 2008: Message edited by: vinora kumar ]
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a large number of servlets I'd rethink the overall architecture, but I still wouldn't worry about object instantiation. The average servlet container instantiates thousands of object before it gets around to instantiating even the first servlet.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic