MY mENU


Friday 16 March 2012

Can you explain Servlet Life Cycle methods ?


Servlet Life Cycle:
Life cycle diagram of servlet:


To perform operations related to these three events & to handle these event there are  3 life cycle methods.
   1.Public void init(-)  [Servlet object is just created.]
   
   2.Public void service (ServletRequest req, ServletResponse res)
                                [Servlet object is ready for request processing]

   3.public void destroy() [Servlet object is about to destroy.]

Life cycle methods of a servlet helps the programmer events that are raised on the servlet object.
Step 1:
                      init(-) is a one time execution method in the life cycle of servlet object. This method executes automatically when underlying web-container creates the servlet object. We place initialization logic in the init(-) method like creating JDBC connection object.

Step 2:
                   service(-,-) method is a repeatedly executing method for servlet object. This method executes automatically when servlet gets the request so we place request processing logic in this method.
If multiple requests are given to a servlet multiple times the service (-,-) method of that servlet executes.

Step 3:
             destroy()  is a one time execution life cycle method. This method executes automatically when servlet object is about to destroy. So we place un initialization logic in the destroy() method like closing JDBC connection.

No comments:

Post a Comment