When a JSP page is accessed for the first time, the server is slower in responding. This is because, every JSP page must be converted into an instance of a Servlet class before it can be used to service client requests. For each request, the JSP engine checks the timestamps of the source JSP page and the corresponding Servlet class file to determine if the JSP page is new or if it has already been converted into class file. Therefore, if we modify a JSP page, the whole process of converting the JSP page into Servlet is performed again. This process consists of the following phases known as life cycle phases.
• Page translation
• Page compilation
• Generated Servlet class loading
• Servlet instantiation phase
• Calling jsplnit() known as initialization phase
• Calling _jspService() known as servicing phase
• Calling jspDestroy() known as destruction phase
Translation phase: – During this phase, the JSP engine reads the JSP page, parses it, and validates the syntax of the tags. Once the validations are completed, the engine creates the java source code that contains public Servlet class.
Compilation Phase:- In this phase, the java compilation unit generated in the previous phase is compiled. The result of this compilation is the ciass file of the container generated Servlet.
Instantiation Phase: – The Servlet class is loaded dynamically and the Servlet engine creates the Servlet instance.
Life Cycle methods of a jsp: – Container generated Servlet implements javax.servlet.HttpJspPage which is a sub interface of JspPage interface. JspPage interface extends javx.servlet.Servlet interface. JspPage interface declares 2 metods.
• jspInit()
• jspDestroy()
HttpJspPage interface declares one method that serves the Http client requests.
• _jspService()
These methods are known as life cycle methods of a jsp
Initialization Phase: – The container calls this method to initialize the Servlet instance. It is called before any other method and is called only once for a Servlet instance. In a jsp we need not generally required to override this method. If required, we can do it in a declaration.
<%!
public void jspInit()
{
//Resources allocation code
} %>
Servicing Phase: – In this phase JspService () method is called by the container every time request comes to the jsp. Container supplies request and response objects to this method. All the template text, JSP expressions and JSP scriptlets are inserted into this method during phase translation time. We never ever override this method in a jsp.
Destruction Phase: – When the container is instructed to take the Servlet instance out of service, before the instance is sent for garbage collection, container calls the life cycle method jspDestroy (). This is the last method that is called on the Servlet instance. As a jsp author, we write clean up code in this method to release the resources allocated to the Servlet in the jspInitQ method. Generally, this method is not required to be overridden. If at all required, we do it in a declaration.
<%!
public void jspDestory()
{
//Resources deallocation code
}
%>
If the container is not instructed to undeploy the Servlet instance, jspDestroy() method is not called at all. jspInit() and jspDestroy() methods are already implemented in the super class of the container generated Servlet. Therefore it is not mandatory to override them in a jsp.
