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
GET
request. Therequest
parameter provides detailed information about the request, and the servlet uses theresponse
parameter to generate the response.-
public void doPost(HttpServletRequest request
,HttpServletResponse response) throws ServletException
,IOException
Called repeatedly to let the servlet process a
POST
request.-
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: ...
Get JavaServer Pages, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.