servlet life cycle

Could you explain servlet life with simple words

i dont want link.

what is the need for public void init method
Manikandan ThiagarajanSenior consultantAsked:
Who is Participating?
Hi This might help you remember better!!

In anology way it is your own life
                                             Being Born -      Grow -              Do some work  --      die

In short Servlet life cycle in        (Loads, Creates ), Initializes , offers Services--   Destroy.

The life cycle of a servlet is controlled by the Web  container

When a request is mapped to a servlet, the Web  container performs the following steps.

                If an instance of the servlet does not exist, the Web container
                                                                         Loads the servlet class.
                Then Creates an instance of the servlet class.

               Then Initializes the servlet instance by calling the init method.

                Invokes the service method, passing a request and response object.
                If the container needs to remove the servlet, it finalizes the servlet by calling
                                                                          the servlet's destroy method.    
Servlet class is first loaded by the class loader then it is initialized by calling init() method and then it will process requests mapped to it and finally it destroys itself by calling destroy() method and get garbage collected.
the init method is called by the servlet container to indicate to a servlet that the servlet is being placed into service.
This way you can do some initializations at the time when your servlet becomes active and available.
Manikandan ThiagarajanSenior consultantAuthor Commented:
what is the use of post method

difference between service and post method
Service method is provided in GenericServlet for handling all sorts of requests like http ftp, etc.
But if you want to handle only Http requests you need to subclass the HttpServlet having Http specific methods as doGet(), doPost(), doHead() etc for handling GET, POST, and HEAD request respectively.
The difference between GET and POST method is that with GET method , the query string is simply appended to the URL of the program when the client issues the request to the server where as with POST method , the data is sent as standard input along with the request body.
In HttpServlet, service() method simply calls the Http specific method depecnding upon the type of request.
For more refer:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.