1. What is the difference between constructor and init() method of servlet? The init() method is typically used to perform servlet initialization--creating or loading objects that are used by the servlet in the handling of its requests. Why not use a constructor instead? Well, in JDK 1.0 (for which servlets were originally written), constructors for dynamically loaded Java classes (such as servlets) couldn't accept arguments . So, in order to provide a new servlet any information about itself and its environment, a server had to call a servlet's init() method and pass along an object that implements the ServletConfig interface. Also, Java doesn't allow interfaces to declare constructors . This means that the javax.servlet.Servlet interface cannot declare a constructor that accepts a ServletConfig parameter. It has to declare another method, like init() . It's still possible, of course, for you to define constructors for your servlets, but in the constructor you don't have access to the ServletConfig object or the ability to throw a ServletException .
2. Do we have constructor in Servlet? Certainly we can have a constructor in servlet class but only default onstructor. We can not have parameterized constructor in servlet because the servlet gets instantiated by the container dynamically and java does not allow to call parameterized constructor for dynamically loaded classes. 3. What is the difference between ServletConext and ServletConfig ? When we deploy a web application the web container reads the deployment descriptor (i.e web.xml ). If there is no problem then the web container creates a ServletContext object for that web application, the web container removes the ServletContext object when the web application is stopped. So the no. of ServletContext objects present in web container depends on no.of web applications present in web.xml .for every web application a ServletContext object is created by web container. WebContainer creats ServletConfig object for every servlet that is a part of web application. As part of web application there will be only one ServletContext object but there can be multiple ServletConfig objects. The web container creates a ServletConfig object while creating a Servlet object.
4. Why do we need a constructor in a servlet if we use the init method? <ul><li>Using init() method , you can initialize things such as db connections, variables to be initialized only once and you can get the parameters from your web.xml file through ServletConfig object. Reasons not to use constructor for initialization </li></ul><ul><li>It is not possible to force the user that you should have constructor with single parameter or two like that. But we can force the methods to be implemented in class through interface by implementing it in class . To achieve this , sun declared init() method to initialize things such as db connections, variables to be initialized only once on servlet interface. if you are going to write any servlet , you must implement the method provided in Servlet interface either explicitly or implicitly. </li></ul><ul><li>Even we can write initialize work inside constructor , but it is not prossible to get parameters declared in web.xml . If you have any bad code on constructor , servlet will not be inistantiated . But at the same time , if you used init() method , servlet will be loaded . But will throw the excpetion to represent servlet has problem in init method. </li></ul><ul><li>Once servlet destroy() method is called , servlet wil be removed from the service. The same servlet will be loaded and initialized again , if new request come to that. Most of the web servers keep the Servlet objects after the destory() method has been called. If new request comes to the same servlet , they will call the init() method directly without creating object ,as they have already had it. </li></ul>
5. Explain the life cycle methods of a Servlet. The interface javax.servlet.Servlet , defines the three life-cycle methods. These are: public void init(ServletConfig config) throws ServletException public void service( ServletRequest req, ServletResponse res) throws ServletException, IOException public void destroy() The container manages the lifecycle of the Servlet. When a new request come to a Servlet, the container performs the following steps. 1. If an instance of the servlet does not exist, the web container * Loads the servlet class. * Creates an instance of the servlet class. * Initializes the servlet instance by calling the init method. 2. The container invokes the service method, passing request and response objects. 3. To remove the servlet, container finalizes the servlet by calling the servlet's destroy method.
6. Explain the directory structure of a web application. The directory structure of a web application consists of two parts. A private directory called WEB-INF A public resource directory which contains public resource folder. WEB-INF folder consists of 1. web.xml 2. classes directory 3. lib directory
7. What is preinitialization of a servlet? A container does not initialize the servlets ass soon as it starts up, it initializes a servlet when it receives a request for that servlet first time. This is called lazy loading. The servlet specification defines the <load-on-startup> element, which can be specified in the web.xml to make the servlet container load and initialize the servlet as soon as it starts up. The process of loading a servlet before any request comes in is called preloading or preinitializing a servlet. Example: The value inside the load-on-startup tag specifies the preference about which servlet will be loaded first. Lower the value means preference of loading is higher. <servlet> <servlet-name>TestServlet</servlet-name> <servlet-class>TestServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet>