JSP page is processed in several phases during its lifecycle. Below are the main lifecycle event which occurs while a JSP file gets processed.
Translation- The JSP file is translated to the Java servlet source. The generated servlet implements additional interface javax.servlet.jsp.HttpJspPage, which defines the JSP-generated servlet lifecycle.
Compilation– Generated Servlet class is compiled using a Java compiler.
Loading– Compiled servlet class is loaded in memory.
Instantiation– The JSP servlet is instantiated by the servlet container.
Initialization– The JSP servlet is initialized, using the JSP API standard HttpJspPage.jspInit(..) method.
Servicing– Request Service is servicing requests by executing the HttpJspPage._jspService() method.
Destruction– The JSP-generated servlet is destroyed, using the HttpJspPage.jspDestroy(..) method.
The first time the file is requested, it is translated into a servlet and then compiled into a servlet class that is loaded into resident memory. The JSP page then becomes a standard Java servlet, and it goes through the same lifecycle steps as we described in the previous section: the servlet is instantiated, initialized, and it finally starts to service the client requests until it’s destroyed. After the loaded JSP servlet services each request, the output is sent back to the requesting client.
The servlet generated from the JSP file implements javax.servlet.jsp.HttpJspPage interface, which is responsible for its lifecycle. This interface is very similar to the servlet lifecycle, but with JSP-specific features. The lifecycle methods of the HttpJspPage interface correspond with the Servlet interface methods: HttpJspPage.jspInit(..) for jsp servlet initialization, HttpJspPage.jspService(..) for request servicing, and HttpJspPage.jspDestroy(..) for jsp servlet destruction.
On all subsequent requests, the server checks to see whether the original .jsp source file has changed. If it has not changed, the server invokes the previously compiled servlet object, skipping the translation and compilation phases. If the source has changed, however, the JSP engine re-parses the JSP source, going to all phases of the JSP lifecycle.