Servlet Life Cycle
-
The
life cycle contains the following steps:
- Load Servlet Class.
- Create Instance of Servlet.
- Call the servlets init() method.
- Call the servlets service() method.
- Call the servlets destroy() method.

- The servlet is initialized by calling the init () method.
- The init method is called only one time,when servlet is first created,never call again for each user request.
- The init() method simply create or load some data that will be used overall life of the servlet.
1. init() method:-
public void init() throws ServletException
{
|
- The servlet container calls the service() method to handle requests coming from the client or browsers and to write the formatted response back to the client.
- The service() method checks the HTTP request type such as GET ,POST etc and calls doGet, doPost etc.
- GET is HTTP method. it works to tell server to get a resource and send it server.
- it should be handled by doGet() method.
- POST ,you can request something and at same time send form data to server.
- it should be handled by doPost() method.
- The destroy() method is called only one time at the end of the life cycle of a servlet.
- It gives opportunity to clean up any resource such as Memory,thread etc.
2. service()method:-
public void service (ServletRequest request,ServletResponse response) throws ServletException, IOException
|
doGet() Method:-
public void doGet (HttpServletRequest request,
HttpServletResponse response)
ServletException, IOException
|
The doPost() Method:-
public void doPost (HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
|
3. destroy() method :-
public void destroy()
|


