Servlet LifeCycle
Container
1) Load class
2) Instantiate servlet (constructor runs)
3) init()
4) service()
5) destroy()
The Container initializes a servlet by loading the class,
invoking the servlet’s no-arg constructor, and calling the
servlet’s init() method.
The init() method (which the developer can override) is
called only once in a servlet’s life, and always before the
servlet can service any client requests.
The init() method gives the servlet access to the Serv-
letConfi g and ServletContext objects, which the servlet
needs to get information about the servlet confi guration
and the web app.
The Container ends a servlet’s life by calling its destroy()
method.
Most of a servlet’s life is spent running a service() method
for a client request.
Every request to a servlet runs in a separate thread!
There is only one instance of any particular servlet class.
Your servlet will almost always extend javax.servlet.http.
HttpServlet, from which it inherits an implementation of
the service() method that takes an HttpServletRequest
and an HttpServletResponse.
HttpServlet extends javax.servlet.GenericServlet—an
abstract class that implements most of the basic servlet
methods.
GenericServlet implements the Servlet interface.
Servlet classes (except those related to JSPs) are in one
of two packages: javax.servlet or javax.servlet.http.
You can override the init() method, and you must override
at least one service method (doGet(), doPost(), etc.).
1) Load class
2) Instantiate servlet (constructor runs)
3) init()
4) service()
5) destroy()
The Container initializes a servlet by loading the class,
invoking the servlet’s no-arg constructor, and calling the
servlet’s init() method.
The init() method (which the developer can override) is
called only once in a servlet’s life, and always before the
servlet can service any client requests.
The init() method gives the servlet access to the Serv-
letConfi g and ServletContext objects, which the servlet
needs to get information about the servlet confi guration
and the web app.
The Container ends a servlet’s life by calling its destroy()
method.
Most of a servlet’s life is spent running a service() method
for a client request.
Every request to a servlet runs in a separate thread!
There is only one instance of any particular servlet class.
Your servlet will almost always extend javax.servlet.http.
HttpServlet, from which it inherits an implementation of
the service() method that takes an HttpServletRequest
and an HttpServletResponse.
HttpServlet extends javax.servlet.GenericServlet—an
abstract class that implements most of the basic servlet
methods.
GenericServlet implements the Servlet interface.
Servlet classes (except those related to JSPs) are in one
of two packages: javax.servlet or javax.servlet.http.
You can override the init() method, and you must override
at least one service method (doGet(), doPost(), etc.).