Table of Contents
Java application to demonstrate servlet life cycle
Servlet life cycle has 3 stages
There are 3 methods in servlet which will get executed automatically once the servlet enters in above stages.
init() : This method will get executed when servlet get initialized.
service() : This method will get executed when servlet starts handling the request. The request can be in form of POST, GET, etc.
destroy() : This method will get executed when servlet is been destroyed.
Java servlet life cycle
ServletLifeCycleDemo.java (Servlet File)
After clicking on the “Click Here” button servlet will start running.
We can see in console that init() and service() method has been executed. These will get executed when the servlet is called.
If we stop the server or restart the server it will force servlet to stop, which means servlet will get destroyed hence servlet will run destroy().
Connect with us on following platforms
PRACTICALS/PRACTICE PROGRAM IN Java