Life Cycle

The JSP/Servlet container is responsible for managing the life cycle of a JSP file. In general there are three important stages in JSP life cycle.

  1. Instantiation
  2. Request Processing
  3. Destruction

JSP Life Cycle – Initialization Stage

When a JSP/Servlet container receives request for a JSP it checks if the JSP is initialized. If the JSP is not initialized already then the container would take the JSP file through the following stages.

# Stage Description
1 Translation Container translate the JSP file into a Servlet file
2 Compilation The generated java Servlet file is compiled into a java Servlet class
3 Class Loading The java Servlet class is loaded into JVM
4 Servlet Instantiation An instance of the Servlet class is created
5 Initialization jspInit() method is called right after instance was created.

jspInit() method is called only once during JSP life cycle. Initialization will make ServletContext and ServletConfig objects available for the Servlet.

JSP Life Cycle – Request Processing Stage

Every request from client is served by _jspService() method. The whole initialization stage is done to make the JSP ready to serve the client request.

JSP Life Cycle – Destruction Stage

A Servlet/JSP container may call the _jspDestroy() method any time after the initialization. Once the _jspDestroy() method is called JSP file has go through to initialization process to be consumed by the client. Container may call the _jspDestroy() method as and when the container is gracefully shutdown or the container need to free up some memory to load other resources. Once the _jspDestroy() method is called the Servlet is no longer referred by a reference and the garbage collector can pick it up.

JSP Life Cycle Methods

  1. _jspInit()
  2. _jspService()
  3. _jspDestroy()

JSP Life Cycle

JSP Life Cycle

Leave a comment

Your email address will not be published. Required fields are marked *