servlet life cycle

Could you explain servlet life with simple words

i dont want link.

what is the need for public void init method
LVL 9
Manikandan ThiagarajanSenior consultantAsked:
Who is Participating?
 
KantiCommented:
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.    
0
 
gksinghietCommented:
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.
0
 
WelkinMazeCommented:
Hi
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.
0
 
Manikandan ThiagarajanSenior consultantAuthor Commented:
what is the use of post method

difference between service and post method
0
 
gksinghietCommented:
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:
http://www.cs.tut.fi/~jkorpela/forms/methods.html#fund
http://medlib.med.utah.edu/kw/advhtml/forms/form3.html
0
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.