Servlet Lifecycle


Hi friends,  in this post we will take an overview of servlet lifecycle.

First of all lets just know what is  servlet?

Servlet is a java program which runs on server side. Servlet interacts with the client via request-response. It is nothing but HTML code embedded into java code.

When any servlet get called container first checks that whether the object of requested servlet is already exists, if not then container instantiates the object and then servlet lifecycle starts with call to init method.

init() – This method gets called only once in lifecycle of the servlet. We can override this method to write the code which we want to execute only once. Object of ServletConfig is passed to this method, which contains the configuration/initialization values stated in web.xml

service() – This method gets called by the container whenever any request comes from the client for that servlet. In this method ServletRequest and ServletResponse objects are passed as arguments to this method. ServletRequest object contains the request from the client.

destroy() – It is called by the server when any servlet is no longer necessary or want to keep the servlet out of service. Container calls this method before removing the object of the servlet.

Thats how servlet lifecycle works.

Happy Coding……..!

About the author

By minakshi.bhosale