File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Servlet unloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet unloading" Watch "Servlet unloading" New topic
Author

Servlet unloading

Stanley Walker
Ranch Hand

Joined: Sep 23, 2009
Posts: 87
I am aware of how and when a servlet is instantiated and initialized.
My question is what happens after the servlet has responded back to the client. Does it immediately get unloaded(thru garbage collection), or does it stay in initialized state(and if so, for how long??)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

A servlet will usually stay in service until the web app is unloaded. The single instance is used to serve all requests to that servlet.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Stanley Walker
Ranch Hand

Joined: Sep 23, 2009
Posts: 87
so does that mean that for n number of requests , there will be n instances of that servlet...
and all this would be retained in the server memory right until the web server is shut down?

or is a pool maintained by the container??? when a request comes in for a resource, a servlet instance is retrieved from this pool?if so, how is this pool maintained and do we have any control over this?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Stanley Walker wrote:so does that mean that for n number of requests , there will be n instances of that servlet...
and all this would be retained in the server memory right until the web server is shut down?


for n number of requests , there will be ONE instance of that servlet.
The container is allowed to unload the servlet if it decides it needs to, and the servlet should ensure that it releases any resources it hold and then reacquire if the servlet is loaded again.
In practice however, the servlet (in my opinion) the container is unlikely to unload a servlet, and the servlet should be stateless so the unload/reload should not be an issue.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

Stanley Walker wrote:so does that mean that for n number of requests , there will be n instances of that servlet...

No. I repeat:
Bear wrote: The single instance is used to serve all requests to that servlet.

The container can pretty much do what it wants, but generally it will use a single instance for the life of a web app.

and all this would be retained in the server memory right until the web server is shut down?

The instance will be retained until the web app (not necessarily server) is unloaded.

or is a pool maintained by the container?

There is no pool.
Stanley Walker
Ranch Hand

Joined: Sep 23, 2009
Posts: 87
thank you for the replies.
let me see if i understand this correctly.
lets say i have a sevlet loginServlet, which is called for logon credentials checking. say user A logs on. lets assume this is the first user logging on. an object of loginServlet will be created and initialized to service user A's request. now if another user B logs on, that same object is used. i guess a thread is started by the container to service client B's request.
am i right ?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

Yes.

The container may load the servlet prior to the request (either because it felt like it or the deployment descriptor told it to), but it is guaranteed to be loaded and initialized to serve the request.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet unloading
 
Similar Threads
way of initializing varialbes in Servlet
about GenericServlet
SOS!!!!
patterns on the cattle drive
Servlet Lifecycle Doubt?