The JSP technology is based on page-compilation. Instead of interpreting the JSP page, the web container converts it into a servlet class, and compiles it. This process typically happens when the web container invokes a JSP pages for the first time in response to a request or at container startup. Web container also allow us to precompile JSP pages into servlets.
The First time a JSP page loaded by the JSP container (also called the JSP engine) , the servlet code necessary to implement the JSP tags is automatically generated, compiled , and loaded int the servlet container. This phase is called translation time. These are following step of JSP page life cycle :
- Translate the JSP to servlet code.
- Comiple the servlet to bytecode.
- Load the servlet class.
- Create the servlet instance.
- Call the jspInit method.
- Call the _jspService method.
- Call the jspDestroy method.
1. JSP Page Translation:
A java servlet file is generated from the JSP source file. This is the first step in its the web container translate the JSP file into a Java source code that contains a servlet class definition. In the translation phase, the container validates the syntactic correctness of the JSP pages and tag files. The container interprets the standard directives and actions, and the custom actions referencing tag libraries used in the page.
2. JSP Page Compilation:
The generated java servlet file is compiled into a java servlet class.
Note: The translation of a JSP source page into its implementation class can happen at any time between initial deployment of the JSP page into the JSP container and the receipt and processing of a client request for the target JSP page.
3. Class Loading:
The java servlet class bytecode is loaded into the web container's JVM using a classloader.
4. Execution phase:
In the execution phase the container manages one or more instances of this class in response to requests and other events.
The interface JspPage contains jspInit()
. The JSP specification has provided a special interface HttpJspPage for JSP pages serving HTTP requests and this interface contains _jspService().
jspInit() method is called immediately after the instance was created. It is called only once during JSP life cycle.
6. _jspService() execution:
Whenever a browser requests a JSP and the page has been loaded and initialized, the JSP engine invokes the jspService method in the JSP. The jspService
method takes an HttpServletRequest
and an HttpServletResponse
as its parameters. It passes the request and the response objects. _jspService()
cannot be overridden.
7. jspDestroy() execution:
method is the JSP equivalent of the destroy method for servlets. Override jspDestroy
when we need to perform any cleanup, such as releasing database connections or closing open files. The jspDestroy
method is declared this way:
<%! public void jspDestroy()
// Your cleanup code goes here.