File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Instantiating Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Instantiating Servlet " Watch "Instantiating Servlet " New topic
Author

Instantiating Servlet

vinora kumar
Greenhorn

Joined: Dec 15, 2008
Posts: 26
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.


vino
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
vinora kumar
Greenhorn

Joined: Dec 15, 2008
Posts: 26
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

"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.


[My Blog]
All roads lead to JavaRanch
vinora kumar
Greenhorn

Joined: Dec 15, 2008
Posts: 26
done
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39535
    
  27
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.


Ping & DNS - updated with new look and Ping home screen widget
vinora kumar
Greenhorn

Joined: Dec 15, 2008
Posts: 26
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
Marshal

Joined: Mar 22, 2005
Posts: 39535
    
  27
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Instantiating Servlet
 
Similar Threads
Servlet init
Invocation init()
init() method on servlets
Servlet query
single instance of servlet (interview question)