In this article, I will explain Servlet and its Life Cycle. To begin with, let us first understand what a servlet is.
What is a Java Servlet?
In fact, the java servlet is a technology to create dynamic web pages. In short, servlets are classes that reside on a web server or application server. Furthermore, a webserver executes a servlet whenever a request arises from the client. In other words, java servlets are the programs that receive requests from clients. Further, the servlets process the request and generate a response object. Finally, this response object is sent back to the client which displays it to the user.
Examples of using Servlets
Since a webserver may hold several servlets, whenever a request arrives from the client, it determines which of the servlets can handle it. Accordingly, it passes the request to the appropriate servlet. For instance, suppose a web page contains two forms as shown below. Also, there are two servlets available – ComputeServlet, and SimpleInterest. These servlets process the specific request that arrived from the client.
<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <form name="f1" method="POST" action="ComputeServlet"> Enter a number: <input type="text" name="t1"/><br/> Enter another number: <input type="text" name="t2"/><br/> <input type="submit" value="Compute"/> </form> <form name="f2" method="POST" action="SimpleInterest"> Enter Principle: <input type="text" name="t3"/><br/> Enter Interest Rate: <input type="text" name="t4"/><br/> Enter Time in Years: <input type="text" name="t5"/><br/> <input type="submit" value="Compute Interest"/> </form> </body> </html>
So, depending upon the request arrives from whichever form, that particular servlet executes. The following figure shows the working of a servlet.
Understanding Servlet and its Life Cycle
To begin with, the servlet life cycle starts with loading the servlet. Therefore, loading the servlet is the first stage in the servlet life cycle. During this stage, the web container loads the servlet class in memory and creates an instance of the servlet class.
Afterward, the servlet initialization takes place. Basically, the web container calls the Servlet.init(ServletConfig) method to initialize the servlet. Also, this method is called only once.
After that, the third stage of handling the request takes place. As a result, the servlet handles the request and response objects in this stage. In the case of an HTTP Servlet, these objects belong to HttpServletRequest and HttpServletResponse classes respectively. Once the request and response objects are created, the Servlet.service(ServletRequest, ServletResponse) method is called. In fact, this method is used to process the request and to generate the response.
Finally, the last stage of the servlet life cycle occurs. It is called destroying the servlet. Since, while the servlet is executing, it may happen that multiple threads have been created. Therefore, this stage waits for all threads to finish the execution. Afterward, the destroy() method is called to destroy the servlet.
The following figure shows the servlet life cycle.