life cycle of jsp

Life Cycle of JSP-: In the website normal web-pages execute on the browser quite easily but web-pages with .jsp extension are executed on the browser after a lot of processes.This life cycle processes until the destroy the file from the creation of the .jsp file.The process of JSP Life Cycle are divided in the following steps

  1. Translation(JSP Code to Servlet Code)
  2. Compilation(Servlet to Bytecode(.class))
  3. Loading(Servlet Class)
  4. Instantiation(Create Servlet Instance)
  5. Initialization(Call jspInt() Method)
  6. Request Processing(Call _jspService() Method)
  7. Destruction(Call jspDestroy() Method)

Translation -: First of thing when the JSP file is created and executed by the JSP engine, then it is converted into a servlet .java. If some error occurs in the .jsp file, it is converted to .java (servlet). in other words we can say that the A Java servlet file is generated from a JSP source file. This is the first phase of the JSP life cycle. In the translation phase, the container verifies the syntax correctness of the JSP page and tag files.

Convert the .jsp file to .java (servlet).

For understanding the we take a simple example

Converted to Servlet


If the .jsp file is executed successfully, a .java (servlet) file is created. The following path is given to see that created .java (Servlet) file.

My JSP file path : ….\webapps\MyProject\netnic.jsp

Create .java(Servlet) file path : …..\work\Catalina\localhost\My\org\apache\jsp

Compilation -: After translating the next process comes compilation. After converting a .jsp file into a servlet, its byte code means .class this file is compiled.

Loading: After the compilation the next process loading the class into memory means after creating that the .class file is loaded into memory.

Instantiation: Then the instance of that .class file means object is created.

Initialization: .class jspInt () This method any request after the object of this file is created; A call occurs before servicing.

public void jspInit(){
//Initialization code
}

Request Processing: When the intialization is done then the _jspService () method gets called by the JSP engine to respond to the request.

_jspService () This method takes two parameters HttpServletRequest this request object and HttpServletResponse this response object.

void _jspService(HttpServletRequest request, HttpServletResponse response) {
//Service code
}


Destruction: This method calls jspDestroy () to destroy the instance (object) of the servlet class after responding.

void jspDestroy(){
//Destruction code
}

Leave a Comment