Top servlet Interview Question Part4 - Tech Interview Club

Latest

interview questions for java,spring,servlet,jsp,hibernate,webservices,.net,html,javascript,andriod,jquery

Comments

Saturday, 24 June 2017

Top servlet Interview Question Part4


7. What are the phases of servlet life cycle?
The web container maintains the life cycle of a servlet instance. Let's see the life cycle of the servlet:
1.    Servlet class is loaded.
2.    Servlet instance is created.
3.    Init method is invoked.
4.    Service method is invoked.
5.    Destroy method is invoked.

Servlet class is loaded
The classloader is responsible to load the servlet class. The servlet class is loaded when the servlet engine itself is started or when the first request for the servlet is received by the web container.

Servlet instance is created
The web container creates the instance of a servlet after loading the servlet class. The servlet instance is created only once in the servlet life cycle.
Init method is invoked
The web container calls the init method only once after creating the servlet instance. Passing a ServletConfig object as argument .The init method is used to initialize the servlet. It is the life cycle method of the javax.servlet. Servlet interface. Syntax of the init method is given below:
public void init (ServletConfig config) throws ServletException  

Service method is invoked
The web container calls the service method each time when request for the servlet is received. If servlet is not initialized, it follows the first three steps as described above then calls the service method. If servlet is initialized, it calls the service method. Notice that servlet is initialized only once. The syntax of the service method of the Servlet interface is given below:
public void service (ServletRequest request, ServletResponse response)   
throws ServletException, IOException  

Destroy method is invoked
The web container calls the destroy method before removing the servlet instance from the service. It gives the servlet an opportunity to clean up any resource for example memory, thread etc. Now the servlet instance is eligible for garbage collection
 The syntax of the destroy method of the Servlet interface is given below:
public void destroy ()  

8. How a servlet is executing without main () method?

If you give a java class directly to JVM/JRE for execution then JVM/JRE expects public static void main (strong args[]) to start the execution. But we are not giving our web application directly to the JVM/JRE. We are hosting our web application in web Container. This web container is already containing main(). So this main() will be execute.
Conclusion : web-server/web-container is also a java application that also contains main() method, so we no need to keep main() in our web application.

9.Can I keep main() method in our servlet class?

You can place but it never executes automatically .because it is not a life cycle method.
You can see main() method acting as helper method(normal method) to life cycle methods.
you can call this main() from any lifecycle method explicitly as a helper method.

No comments:

Post a Comment