Nearby lessons
3 of 30JSP - Life Cycle
- Understand each phase of the JSP life cycle
- Know when translation and compilation happen
- Learn how pre-compilation improves performance
JSP Life Cycle covers how a JSP goes from a .jsp file to a running servlet. This lesson explains each phase of the lifecycle and pre-compilation to optimise first-request performance.
Life Cycle of JSP
A JSP page goes through the following phases from first request to destruction:
- Translation Phase —
.jsp→.java(servlet source) - Compilation Phase —
.java→.class(bytecode) - Servlet Class Loading — load the generated class into the container
- Servlet Instantiation — create an instance of the generated servlet
jspInit()— called once for initialization_jspService()— called for every requestjspDestroy()— called once before the servlet is taken out of service
First Request vs Subsequent Requests
For the first request — all phases execute in order (translation, compilation, loading, instantiation, jspInit, _jspService).
For subsequent requests — only _jspService() is called (since the servlet is already loaded and initialised).
When Does Translation Happen?
JSP will participate in the Translation Phase in the following cases:
- At the time of the first request
- If the source code of the JSP has been modified compared to earlier requests. The JSP Engine uses a tool (like ARAXIS) to compare timestamps of the
.classand.jspfiles.
Pre Compilation of JSP
For the first request, the following activities are performed:
- Translation
- Compilation
- Class Loading
- Instantiation
jspInit()
For the second request onwards, only _jspService() is executed.
This means the first request is slower than subsequent requests.
To overcome this problem, we can use pre-compilation of JSP.
How to Trigger Pre Compilation
Invoke the JSP with the special query parameter:
What Pre Compilation Does
The jsp_precompile=true request is not a real request to the JSP — it only performs pre-compilation. This triggers:
- Translation
- Compilation
- Class Loading
- Instantiation
jspInit()
Then, when the first real request arrives, only _jspService() executes.
Advantage of Pre Compilation
The main advantage: all requests (including the first) are processed with uniform response time. The expensive translation and compilation happen before any real user hits the page.
JSP Page Elements Overview
A JSP page can contain the following elements:
| Category | Elements |
|---|---|
| Template Text | Plain HTML and static content |
| Directives | page, include, taglib |
| Standard Actions | jsp:useBean, jsp:setProperty, jsp:getProperty, jsp:include, jsp:forward, jsp:param, jsp:plugin, jsp:fallback, jsp:params |
| Scripting Elements | Scriptlet, Expression, Declaration |
| Expression Language | ${...} |
| Comments | JSP, HTML, Java |
- Translation happens on the first request (or when the source changes)
- Pre-compilation eliminates first-request latency
- Only _jspService() runs after pre-compilation