Thursday, January 19, 2017

JSP Life Cycle

In this post we will learn JSP flow of execution. Before going to learn JSP flow of execution first we will know the importance of JSP technology and where we used JSP in the IT projects. JSP is a server side technology,it can be used to prepare presentation/view part in the web application development. JSP pages can be deploy at any place in the web application directory structure. we can deploy JSP pages under the application folder or we can deploy under classes folder. But it highly recommendable deploy jsp pages under application folder,if we deploy jsp pages under application folder we can access that JSP page directly with its name just like html page,if we deploy jsp page under WEB-INF folder then we have to configure jsp page in web.xml file with a particular URL pattern. By using that url pattern only we can access jsp page.

Now let us discuss the flow of JSP Life cycle of execution.

JSP server page is slightly different life cycle than servlets. Servlets are fully compiled with java classes whereas JSP must be compiled into servlets before they can be accessed. This compilation step is handled by the JSP container at run time. 

Life Cycle methods of JSP:
1) jspInit()
2) jspService()
3)jspDestroy()

The three life cycles of jsp internally uses the 3 life cycles of servlet,since jsp container is developed based on servlet container.

JSP container never calls JSP life cycle methods directly. Actually jsp container internally activates servlet container and this servlet container calls 3 servlet life cycle methods and these life cycle methods internally calls jsp life cycle methods as shown below.

Recommended to Read: Life cycle of Servlet

With respect to tomcat 6.0:

public class First.jsp extends org.apache.jasper.runtime.HttpJspBase
{
public void -jspInit()

{
..........
.........
}
public void -jspService()
{
..............
.............
}
public void -jspDestroy()
{
.............
.............
}
}
//super class of jsp equivalent servlet program
public class HttpJspBase extends HttpServlet
{
public void init(ServletConfig cg)
{
super.init(cg);
-jspInit();
}
public void destroy()
{
-jspDestroy();
}
public void service(HttpServletRequest req,HttpServletResponse res)
{
-jspService(req,res);
}
.............
..............// other methods
}

The above code proves two concepts. They are
a) Every JSP equivalent servlet is HttpServlet, JSP container internally uses Servlet Container.
b) JSP life cycle methods will be executed by servlet container through Servlet life cycle methods. 



When a JSP page is requested, the container looks for a Servlet class. If the class does not exist, or if the servlet compile date is older than the last access date on the JSP, the JSP compiled. When a JSP file is accessed, the container converts it into a java class that implements the javax.servlet.jsp.HttpJspPage interface. The various JSP building blocks are translated into java code and compiled into a servlet.




No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...