Loading The Servlet Class: When the web container gets the request from the browser for servlet then servlet class will be loaded.
Creating Servlet Instance: The web container creates an object for the servlet when servlet class is loaded, here the important point is an object and will be created in life cycle only once.
Init Method: This method will be called by the web container after an object was created to the servlet. The functionality of the
init() method is to initialize the servlet, following is the regular syntax to declare the
init() method.
[java]public void init(ServletConfig config) throws ServletException[/java]
Service Method: The
service() method will be called by web container when servlet will have the request, in case servlet is not initialized then above methods will be utilized. In the life cycle of servlet this method will be utilized only once. Following is a syntax to declare the
service() method.
[java]public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException [/java]
Destroy Method: Web container is the responsibility to call this method utilized while vanishing an object of the Servlet from the service, it provides the chance for servlet to clean up the memory or junk data. Following is the Syntax for the destroy method.
[java]public void destroy()[/java]
Following is the code structure of life cycle methods.
Can user call servlet destory() from service()?
The
destory() is part of servlet life cycle methods, it is used to kill the servlet instance. Servlet Engine is used to call
destory(). In case, if user call destory method from service(), it just execute the code written in the destory(), but it wont kill the servlet instance. The destroy() will be called before killing the servlet instance by servlet engine.