Servlets have 5 phases in their lifespan, as follows:
This is an ordinary constructor call by the server.
The server who created the servlet calls the init method
somewhere between creation and the first request it ever gives
the servlet to handle.
For every incoming request the server calls the service method.
The server packages all the request data in a ServletRequest object, and
creates a ServletResponse object for the servlet to write reply data to.
Note that the service method is run in a seperate thread.
This is also the great advantage of using servlets versus traditional cgi
scripting: instead of forking of a proces for every request only a new
thread is created.
This method is called by the server indicating that the server no longer
requires this servlet's services. The serlvet is expected to release any
resources it is holding using this method.
(With resources things like database connections etc are meant).
This happens whenever the garbage collector happens to feel like
reclaiming the memory used by this servlet.
Initializes the servlet.
Called by the server exactly once during the lifetime of the servlet.
This method can be used to setup resources (connections to a
database for example) for this servlet. The servlet should store the
ServletConfig so it can return it again when the
getConfig() method is called. If the the servlet is
temporarily or permanently unavailable it should throw an
Called by the server every time it wants the servlet to handle
a request. The servlet engine doesn't have to wait until the service
call is finished but can start another thread and call the service method
again to handle multiple concurrent requests. If a servlet doesn't want
this to happen it has to implement the SingleThreadModel
request - all the request information
response - class to write all the response data to