Life Cycle of JSP

The Life cycle of a JSP means a series of changes JSP passes through its from beginning till the end. In this article, we will discuss the Life cycle of JSP in detail.

What is JSP Life Cycle?

JSP page has to be converted into Servlet first in order to process the request. So the JSP Life cycle involves the conversion of the JSP page into Servlet. Thus the Life cycle will involve different phases from its creation and destruction. The main component here is the JSP Engine.

What is JSP Engine?

A JSP Engine is a container that translates, executes, and processes the JSP Pages and delivers requests to them. The JSP container is executed by a servlet container.

A JSP container is incorporated into a Web Server. It provides a runtime Environment for JSP Pages.

Stages involved in JSP Life Cycle

When the request for the JSP page is received by the browser, it checks whether it needs to compile the page. If there is any modification in the JSP page the JSP Engine will first compile the page. A compilation involves the following three steps.

  • Resolving of JSP Page
  • Converting JSP page to Servlet.
  • Compiling the Servlet.

Steps in JSP Life Cycle

  • Conversion/ Translation of JSP Page.
  • Compilation of JSP Page.
  • Loading of Class.
  • Instantiation of generated Servlet.
  • Initialization
  • Servicing of request.
  • Destruction by the container.

Let’s see every step in detail

Translation of JSP

This is the first step of the Life cycle of JSP that involves the conversion of the java Source file into Java Servlet File.

  • In this phase, the container validates and checks the correctness of JSP pages.
  • The JSP Engine validates the custom tags and standard directive used in the JSP page(We will discuss in the coming tutorial).
  • In the below Example of hello.jsp, the JSP page is translated into
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Life Cycle</title>
String fname="Nicolas";  //Scriptlet Tag
  <% out.println("Hello "+fname); %>
Public class hello_jsp extends HttpServlet{
Public void _jspservice(HttpServletRequest req, HttpServletResponse res) Throws IOException,ServletException
PrintWriter out=res.getWriter();
String fname="Nicolas";
out.write("Hello ");


In the above example,

  • In the hello.jsp page, we have declared and initialize a  string variable “fname” with the name “Nicolas” and it is printed on the browser.
  •  JSP Engine loads the page and converts it into the servlet
  • All the text is converted to println() and all JSP Element is converted into Java code.

Compiling JSP Page

This is the second stage in the Life cycle of JSP that involves the compilation of generated Java File.

  • The Java class now compiled into a java servlet class.
  • The translation can happen at any time between the deployment of the JSP Page and the processing of JSP Page.
  • The compile to hello_jsp.class files.
  • The Servlet class that generated is loaded into the container.


This is the next stage of the Life Cycle of JSP that involves the creation of the object of a class.

  • The instance of the class is created.
  • The Container manages the instances of the classes in response to request and response.
  • The JSP Engine provides methods such as _jspinit() and _jspdestroy().


In this step, the initialization of the object is done.

public void jspInit()
  //initialize the given code
  • _jspinit() method will initialize the instance of the servlet object created.
  • After creating instance init() method is called.
  • As like servlet, this method is called only once in the Life Cycle.


In this, the request and response object of the JSP Page is handled using the _jspservice() method.

void _jspservice(HttpServletRequest req HttpServletResponse res)
  //handle all request and responses
  • The container invokes the _jspservice() method to handle the request raised by the JSP page.
  • This method handles the GET, POST request just like the servlet service() method.
  • It can be called multiple times.


In this, the method _jspdetroy() is run to remove the instance of the servlet class.

public void _jspdestroy()
  • The container invoked the _jspdestroy() method.
  • Whenever the cleanup operation is involved in class this method gets invoked.
  • It is used to perform operations such as closing connection, closing database connection.

Thus this takes us to the end of the Life Cycle of JSP. We have seen all the phases involved in the Life cycle which might have given you a clear understanding of how JSP pages are handled.

In the next article of this tutorial, We will look at the Elements of JSP in detail such as tags used in JSP like Scriptlet, Declaration, Expression, and so on.