Oct 19

Servlets Tutorial 4 : Explain life cycle of Servlets?

Servlets follow three phase life cycle initialization,service and destruction.

1) init() method :

1) init() method : init method is called when the servlet is first created.

2) init() method is called exactly once after the instantiating Servlet.

3) init() method is used to write initialization code such as loading database drivers and initialization values so on.

4) When invoking this method servlet container passes a Servlet-Config.

5) One of the most important and common task that init() method performs is reading server specific initialization parameters.

6) The init method is important  because the servlet container passes a ServletConfig object, which contains the configuration values given in the web.xml file.

Signature : public void init(ServletConfig servletConfig) throws ServletException

init() method can throw servlet exception.When init() method throws Servlet exception servlet container does not executes service() method.

servletException is also thrown when the method does  not return with in the time period.

Continue reading