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

Oct 19

Servlets Tutorial 2 : Explain Servlet api and its methods?

Explain Servlet api and its methods ?

1) All Servlets must  implement javax.servlet.Servlet interface either directly or indirectly.

2) Servlet container will create an instance of Servlet class and uses it to handle incoming requests.

3) Servlet interface defines methods that all servlets must implement.

4) Servlet class defines a contract between Servlet and Servlet container.

Continue reading