05 - JSP Page Life Cycle

5.1 Overview

In previous chapters we have discussed about the JSP introduction and architecture and in this chapter we will discuss the life cycle of a JSP page. It is very important for a JSP developer to understand the life cycle of a JSP in details so you need to pay attention.

5.2 JSP page life cycle

There are six phases from which each JSP page has to go through in its entire lifecycle.

1. Page Translation

2. Page Compilation

3. Load Class and Create Instance

4. JSP initialization (Call jspinit() )

5. JSP execution (Call _jspServcie())

6. JSP clean up (Call jspDestroy() )

 

jspInit() , _jspService() and jspDestroy() are called as JSP life cycle methods.

Each JSP page is converted to Servlet before it actually serve the client requests. There are two scenarios in which JSP engine translate the JSP to servlet and they are

- JSP Page is not translated to servlet (or we can say if corresponding servlet is not available )

- If timestamp of JSP page is new as compared to that of translated servlet which means jsp file has been modified after the servlet was created out of it.

Therefore whenever we modify a JSP page , whole process of converting JSP in to a Servlet is performed again.

You must have observed that loading of a jsp page for the first time takes long as compared to subsequent requests. The reason is because jsp engine performs entire process of translation to compilation but with subsequent request, these steps are by passed.

5.2.1 Page Translation

  – As the name implies , a java servlet file is generated from the JSP source file. During translation phase –

  -  JSP engine reads the jsp file and parse it.

  - the container validates the syntactic correctness of the JSP page and tag By tags we mean standard directives and actions along with the custom actions referencing tag libraries used in the page

If this phase any errors related to Tags are detected. For example if we add the import statement like below, we will get an error ( spelling mistake of import )

    <%@ page imort = “ java.util.*” %>

In below figure we can see that a request for sample.jsp is translated by translator to generate sample_jsp.java file

5.2.2 Page Compilation

This phase compiles the java file generated in translation phase. In this phase all the syntax of java code are validated. We can use the java code in jsp using scriplet , expression or declaration so any syntax errors in java code are detected. For example if we use

<%= variable ; %> in jsp file , we will get a compilation error because this statement has a comma at the end in Expression tag. Such kind of errors are detected in this phase and if everything is ok , class file is generated.

Below figure shows that sample_jsp.java file generated is compiled by compiler to generate a sample_jsp.class file.

5.2.3 Loading class and Instantiation

Once the class is compiled , container loads the class and create a instance of it. In below figure we can see sample_jsp.class is loaded and an instance is created

 

 

Like us on Facebook