WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JSP life cycle


The key to understanding the underlying capabilities of JSP is to understand the lifecycle they follow.

The JSP lifecycle is the entire process from creation to destruction, similar to the servlet lifecycle, except that the JSP lifecycle also includes compiling JSP files into servlets.

Here are the stages that have been taken during the JSP lifecycle:

  • Compile stage:

    The servlet container compiles the servlet source file and generates the servlet class

  • Initialization phase:

    Load the servlet class corresponding to the JSP, create its instance, and call its initialization method

  • Execution phase:

    Invoke the service method of the servlet instance corresponding to JSP

  • Destruction phase:

    Invoke the destruction method of the servlet instance corresponding to the JSP, and then destroy the servlet instance

Obviously, the four main phases of the JSP life cycle are very similar to the servlet life cycle. The following is an illustration:


JSP compilation

When the browser requests a JSP page, the JSP engine first checks to see if it needs to be compiled. If the file has not been compiled, or has been changed since the last compilation, compile the JSP file.

The compilation process consists of three steps:

  • Parse the JSP file.
  • Convert the JSP file to a servlet.
  • Compile the servlet.

JSP initialization

After the container loads the JSP file, it will call the jspInit() method before providing any services for the request. If you need to perform a custom JSP initialization task, overriding the jspInit() method will do the same, like this:

public void jspInit(){
  // Initialization Code
}

Generally speaking, the program is only initialized once, and so is the servlet. Normally you can initialize database connections, open files, and create lookup tables in the jspInit() method.


JSP execution

This phase describes all request-related interactions in the JSP lifecycle until it is destroyed.

When the JSP page is initialized, the JSP engine will call the _jspService() method.

The _jspService() method takes an HttpServletRequest object and an HttpServletResponse object as its arguments, like this:

void _jspService(HttpServletRequest request,
                 HttpServletResponse response)
{
   // server-side processing code
}

The _jspService() method is called once in each request and is responsible for generating a response corresponding to it, and it is also responsible for generating responses for all seven HTTP methods, such as GET, POST, DELETE, and so on.


JSP cleanup

The destruction phase of the JSP lifecycle describes what happens when a JSP page is removed from the container.

The jspDestroy() method is equivalent to the destruction method in the servlet in JSP. Overwrite the jspDestroy() method when you need to perform any cleanup work, such as releasing a database connection or closing a folder.

The format of the jspDestroy() method is as follows:

public void jspDestroy()
{
   // Clean up code
}

Instance

The JSP lifecycle code example is as follows:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
<title>life.jsp</title>
</head>
<body>

<%! 
  private int initVar=0;
  private int serviceVar=0;
  private int destroyVar=0;
%>
  
<%!
  public void jspInit(){
    initVar++;
    System.out.println("jspInit(): JSP is initialized"+initVar+"Times");
  }
  public void jspDestroy(){
    destroyVar++;
    System.out.println("jspDestroy(): JSPDestroyed"+destroyVar+"Times");
  }
%>

<%
  serviceVar++;
  System.out.println("_jspService(): JSP responded"+serviceVar+"Requests");

  String content1="Initialization times : "+initVar;
  String content2="Respond to customer requests : "+serviceVar;
  String content3="Number of destruction : "+destroyVar;
%>
<h1>jsp tutorial JSP test example </h1>
<p><%=content1 %></p>
<p><%=content2 %></p>
<p><%=content3 %></p>

</body>
</html>