Pr.Pg Next Pg

JSP tutorials life cycle

  • The life cycle of JSP is controlled by the three methods which are automatically called when a JSP is requested and when the JSP terminates normally. This methods are called after JSP page is translated to the corresponding servlets, compiled to bytecode, class file is loaded in container and servlet instance is created.

 

JSP life cyclce

JSP Life cycle

 

JSP page translation

  • JSP servlet engine parse JSP source file into corresponding servlet. This is the first step in multiple phase life cycle.

  • In the translation phase the container validates the syntactic correctness of the JSP pages and tag files.

  • The container interprets the standard directives and actions and the custom actions referencing tag libraries used in the page.

 

JSP page compilation

  • The generated servlet file is then compiled into a java class file containing byte code.

 

Loading a servlet class

  • The Servlet class which was compiled from the JSP source file is loaded into the web container.


Servlet instance creation

  • Servlet instance is created when the servlet is loaded for the first time in the web container.

  • Web container can manage one or more instance of class in response to request and other events.

 

jspInit()

  • First step in JSP life cycle when JSP page is requested and used to initialize objects and variables that are used throughout the life of the JSP.

  • This method is similar to the init() method of Java Servlet and also in Applets.

  • The jspInit() method look like below:

public void jspInit()

{

//initialization code…

}

 

_jspService()

  • _jspService method is called for every request of the JSP page during its life cycle. It also calls the doGet() or doPost() method of servlet created.

  • _jspService() method takes two parameter HttpServletRequest and HttpServletResponse as below:

public void _jspService(HttpServletRequest req, HttpServletResponse res)

{

// Service handling code..

}

 

jspDestroy()

  • When JSP terminates normally at that time the jspDestroy() method is called automatically by the web container.

  • This method is not called if JSP terminates abnormally.

  • It is used for cleanup memory which is utilized by resources used during execution of the JSP.

  • The jspDestroy() method has the following signature:

public void jspDestroy()

{

//cleanup code..

}

Pr.Pg border                                              Next Pg