Servlet Lifecycle
The web
container manages all aspects of the
servlet’s lifecycle. It creates an instance of the
servlet class when needed, passes requests to the instance for
processing, and eventually removes the instance. For an
HttpServlet, the container calls the following
methods
at
the appropriate times in the servlet lifecycle:
-
public void init( ) throws ServletExecption Called once, before the first request is delivered. This method can be used to initialize the servlet’s state, for instance by reading initialization parameters defined in the web application deployment descriptor and saving the values in instance variables to be used during request processing.
-
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException Called repeatedly to let the servlet process a
GETrequest. Therequestparameter provides detailed information about the request, and the servlet uses theresponseparameter to generate the response.-
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException Called repeatedly to let the servlet process a
POSTrequest.-
public void destroy( ) Called once, before the servlet is taken out of service. This method can be used to save to permanent storage data accumulated during the servlet’s lifetime and to remove references to objects held by the servlet.
Besides the doGet( ) and doPost(
) methods, there are methods corresponding to the other HTTP methods: ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access