Nearby lessons

3 of 30

JSP - Life Cycle

📌 What You Will Learn
  • 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:

  1. Translation Phase.jsp.java (servlet source)
  2. Compilation Phase.java.class (bytecode)
  3. Servlet Class Loading — load the generated class into the container
  4. Servlet Instantiation — create an instance of the generated servlet
  5. jspInit() — called once for initialization
  6. _jspService() — called for every request
  7. jspDestroy() — 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).

Example02
JCode Cell
1 
2First Request:
3 Translation → Compilation → Class Loading → Instantiation
4 → jspInit() → _jspService() → jspDestroy()
5 
6Second Request Onwards:
7 _jspService() ← only this runs
8

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 .class and .jsp files.

Pre Compilation of JSP

For the first request, the following activities are performed:

  1. Translation
  2. Compilation
  3. Class Loading
  4. Instantiation
  5. 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:

Example05
JCode Cell
1 
2http://localhost:7777/jspdapp1/demo.jsp?jsp_precompile=true
3

What Pre Compilation Does

The jsp_precompile=true request is not a real request to the JSP — it only performs pre-compilation. This triggers:

  1. Translation
  2. Compilation
  3. Class Loading
  4. Instantiation
  5. 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:

CategoryElements
Template TextPlain HTML and static content
Directivespage, include, taglib
Standard Actionsjsp:useBean, jsp:setProperty, jsp:getProperty, jsp:include, jsp:forward, jsp:param, jsp:plugin, jsp:fallback, jsp:params
Scripting ElementsScriptlet, Expression, Declaration
Expression Language${...}
CommentsJSP, HTML, Java
📝 Key Takeaways
  • Translation happens on the first request (or when the source changes)
  • Pre-compilation eliminates first-request latency
  • Only _jspService() runs after pre-compilation

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3