GeeCON Prague 2014*
The moose likes Servlets and the fly likes Servlet Lifttime Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Lifttime" Watch "Servlet Lifttime" New topic
Author

Servlet Lifttime

Ramkumar Subburaj
Ranch Hand

Joined: Sep 07, 2007
Posts: 83
Hello,

Suppose i configure my web.XML such that my servlet is loaded during the server start up,

does the servlet instance live till the server stops or does the servlet instance is killed after a specific amount of time without any request and waits for a new request to create a new servlet instance.

i know there will be only one servlet instance per application but i want to know the lifetime of a servlet instance.


SCJP 1.5, SCWCD 1.4.
Hanging between Web Services and EJB
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42028
    
  64
That's up to the servlet container. It can create and destroy servlet instances whenever it feels like it. The most common case is probably that one servlet instance gets created at startup and is then used until the web app stops, but that's not specified.

Also, I don't think that the spec requires that there only be a single servlet instance. Again, that's the common case, but I think the container is free to create several instances that exist simultaneously.

The lesson is: don't assume that the "init" method is only called once during the lifetime of a web app, and make sure that everything permanent that it sets up is torn down in the "destroy" method.


Ping & DNS - my free Android networking tools app
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Originally posted by Ulf Dittmer:
The lesson is: don't assume that the "init" method is only called once during the lifetime of a web app


so you mean that servlet init method may call more than once ,it depends on container right?
Ramkumar Subburaj
Ranch Hand

Joined: Sep 07, 2007
Posts: 83
thanks for your reply and surprised to know that there would be more than one server instances for a servlet.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42028
    
  64
so you mean that servlet init method may call more than once ,it depends on container right?

Yes. Of course, the init method of any particular servlet instance will only be called once, as per the Servlet interface documentation.

surprised to know that there would be more than one server instances for a servlet.

Not necessarily would - but potentially could.
[ November 11, 2008: Message edited by: Ulf Dittmer ]
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Thanks Ulf. really it is surprise to me
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Unless you're using the deprecated SingleThreadModel interface or hosting in a distributed environment, there will be only one instance of your servlet at a time. The container is free, however, to take a servlet out of service, and to put it back into service at any time.

Each time a servlet is put into service, its init method will be called.

From the servlet spec:

SRV.2.2 Number of Instances

The servlet declaration which is part of the deployment descriptor of theWeb application
containing the servlet, as described in Chapter SRV.13, �Deployment
Descriptor�, controls how the servlet container provides instances of the servlet.
For a servlet not hosted in a distributed environment (the default), the servlet
container must use only one instance per servlet declaration.
However, for a servlet
implementing the SingleThreadModel interface, the servlet container may
instantiate multiple instances to handle a heavy request load and serialize requests
to a particular instance.

In the case where a servlet was deployed as part of an application marked in
the deployment descriptor as distributable, a container may have only one instance
per servlet declaration per Java Virtual Machine
(JVMTM). However, if the servlet
in a distributable application implements the SingleThreadModel interface, the
container may instantiate multiple instances of that servlet in each JVM of the
container.



[ November 11, 2008: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Originally posted by Ben Souther:
Each time a servlet is put into service, its init method will be called.


so you mean every request,will init call ?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

No.
Init is called only when a servlet is put into service.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Originally posted by Ben Souther:
No.
Init is called only when a servlet is put into service.


But ben, i am confusing when the servlet is put into service? actually what is the servlet is put into service?

i think when ever new instance created then servlets init call. that mean "the servlet is put into service=new instance created"
Rodrigo Tomita
Ranch Hand

Joined: Apr 28, 2008
Posts: 70
Seetharaman,

I believe that what Ben meant by "put into service" is "ready to accept requests". So, we would have:

1 - servlet (class) instantiation
2 - servlet ready to accept requests (init called)
3 - eventually request received (doGet or doPost called)

right?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Yes, 'put into service' means loaded, instanciated, and finally, initialized.
Once in service, your servlet is ready to handle any incoming requests.

If you haven't already, you should read the Servlet spec (link in my signature). In particular look at SRV 2.3 which covers the servlet life cycle.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Thanks Ben
 
GeeCON Prague 2014
 
subject: Servlet Lifttime