• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet unloading

 
Stanley Walker
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64721
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stanley Walker
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64721
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64721
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic