How to run the above program ?
It is important to make sure that you have some server like Apache Tomcat installed and configured with the IDE of your choice like Netbeans.
Now, if the above condition is fulfilled then you can simply create the above three files under Web application project and then simply run the above application.
First of all the index.html file gets executed and then when the button is clicked the request goes to the Servlet, in this case LifeCycleServlet and the service() method handles the request.
When the above invoke life cycle servlet button is clicked the code under service() method of LifeCycleServlet is executed and the below output is obtained :
Starting with first Servlet Application
To get started with Servlets, let’s first start with a simple Servlet application i.e LifeCycle application, that will demonstrate the implementation of the init(), service() and destroy() methods.
First of all it is important to understand that if we are developing any Servlet application, it will handle some client’s request so, whenever we talk about Servlets we need to develop a index.html page (can be any other name also) which will request a particular Servlet to handle the request made by the client (in this case index.html page).
To be simple, lets first describe the steps to develop the LifeCycle application :
- Creating the index.html page
- Creating the LifeCycle Servlet
- Creating deployment descriptor