A JSP page is not executed directly.
In order for a JSP page to handle user requests, every JSP page is first translated to a Servlet and has to go through a few more phases before it could actually take user requests and respond
appropriately. Let's discuss these phases in the life of every JSP page or also known as Lifecycle of a JSP page. These stages or phases are performed in the
order in which they are mentioned below.
JSP Page Translation.
Loading and Initialization.
JSP Page Translation
In this phase, a Web Container translates a JSP web page to its equivalent Java code file,
which is also known as a Servlet. The main purpose of this phase is to translate a JSP page which majorly contains HTML code mixed with some Java code,
to a Servlet code file, which majorly contains Java code mixed with some HTML code. This servlet code file is easily executed by the Java Virtual Machine(JVM).
JSP page translation process takes place in either of these two situations -
When a JSP page has to serve to a client's request for the first time, it is translated to a Servlet code.
When a JSP page is updated or changed and it has to redeployed by the Web Container.
After the JSP Page Translation phase is over and a Servlet code file is generated from a JSP web page, this Servlet code file is compiled and
checked for the errors by the Web Container. In this stage, the Java Servlet code file is compiled and converted to Java bytecode(.class) file by the
Web Container. After the compilation state is over, the Servlet .class is ready to be loaded and initialized in the next phase of JSP lifecycle.
Loading and Initialization
After the JSP page is translated to a Java Servlet code and this Servlet code is successfully compiled to a Java Servlet bytecode(.class) file, it is time for
Web Container to perform Loading and Initialization phase in two steps -
Loading the Servlet - The Servlet bytecode(.class) file is loaded by the Web Container.
Initializing the Servlet- Next, an instance of Servlet class is created when the Web Container calls the no-argument of Servlet class. This no-argument is added to the Servlet class code
by the Web Container when the JSP page is translated to its equivalent Servlet code.
This object of Servlet class is then initialized by the Web Container, in order for the Servlet class to handle the user requests in the next phase.
The next phase in JSP lifecycle is handling the client requests by using the instance of Servlet class which is successfully initialized in the previous step and performing next two steps -
Creating the ServletRequest and ServletResponse instances - Based on whether the client is sending requests using the HTTP protocol or a NON-HTTP protocol,
Web Container creates the instances of ServletRequest and ServletResponse or it creates instances of HTTPServletRequest and HTTPServletResponse.
Calling the service() method - Next, the Web Container calls the service() method of JSP, which eventually calls the _jspService() method present in the translated Servlet class and the
appropriate request and response objects are passed to this method, in order to handle client's request and respond appropriately.
Destroying the instance of Servlet class is the last phase in the lifecycle of a JSP web page. In this phase, the Web Container performs the following steps -
It allows all the current threads to complete their execution of the service() method on the Servlet object.
After all the threads have successfully completed their execution of service() method, the Web Container calls the destoy() method on the Servlet instance, to indicate to a Servlet that it is being taken out of service.