Skip to content
Home » Life Cycle of JSP

Life Cycle of JSP


Life Cycle of JSP

Introduction

The JSP life cycle defines the sequence of phases through which a JSP page passes from the time it is requested by a client until it is destroyed by the server. Internally, a JSP page is not executed directly; instead, it is translated into a servlet, compiled, and then managed by the servlet container. Therefore, the JSP life cycle is closely related to the servlet life cycle.


Phases of JSP Life Cycle

Image
Image
Image

The JSP life cycle consists of the following main phases:

  1. Translation
  2. Compilation
  3. Class Loading
  4. Instantiation
  5. Initialization
  6. Request Processing
  7. Destruction

1. Translation Phase

When a JSP page is requested for the first time, the JSP engine translates the JSP file into a Java servlet source code.

Key Points

  • JSP tags, directives, and scriptlets are converted into servlet code
  • Static HTML is converted into out.write() statements
  • The generated servlet file is stored internally by the container

This phase occurs only once, unless the JSP file is modified.


2. Compilation Phase

After translation, the generated servlet source code is compiled into bytecode (.class file).

Key Points

  • Compilation errors occur here if JSP syntax is incorrect
  • If compilation fails, the JSP page cannot be executed

This phase is also executed only once, unless the JSP file changes.


3. Class Loading Phase

The compiled servlet class is loaded into memory by the class loader of the servlet container.

Key Points

  • The servlet class becomes available for execution
  • No JSP-specific method is executed at this stage

4. Instantiation Phase

The servlet container creates an instance of the generated servlet class.

Key Points

  • Only one instance is created per JSP page
  • This instance handles multiple client requests using multithreading

5. Initialization Phase – jspInit()

After instantiation, the container calls the jspInit() method.

public void jspInit()

Purpose

  • Perform initialization tasks
  • Allocate resources such as database connections

Key Points

  • Called only once
  • Similar to the init() method of a servlet

6. Request Processing Phase – _jspService()

For every client request, the container calls the _jspService() method.

public void _jspService(HttpServletRequest request,
                        HttpServletResponse response)

Purpose

  • Process client requests
  • Generate dynamic responses

Key Points

  • Called multiple times
  • Cannot be overridden by developers
  • Handles both GET and POST requests internally

7. Destruction Phase – jspDestroy()

When the JSP page is removed from service, the container calls jspDestroy().

public void jspDestroy()

Purpose

  • Release allocated resources
  • Perform cleanup operations

Key Points

  • Called only once
  • Executed during server shutdown or application redeployment

JSP Life Cycle Flow (Textual Representation)

JSP Request
    ↓
Translation
    ↓
Compilation
    ↓
Class Loading
    ↓
Instantiation
    ↓
jspInit()   → (once)
    ↓
_jspService() → (for every request)
    ↓
jspDestroy() → (once)

Important Characteristics of JSP Life Cycle

  • JSP is converted into a servlet internally
  • Initialization and destruction occur only once
  • Request processing is multithreaded
  • Performance improves after the first request
  • Developers do not control _jspService()

Conclusion

The JSP life cycle explains how a JSP page is processed and executed within a Java web application. Starting from translation and compilation to request handling and destruction, each phase is managed by the JSP engine and servlet container. Understanding the JSP life cycle helps developers write efficient JSP pages, optimize performance, and manage resources effectively in Java-based web applications.