Sunday, January 23, 2011

JSP Tutorial -1 : Life Cycle of a JSP Page


Life of the the jsp page is  same as the servlet life cycle.
Once translated the jsp file is just like a servlet.

The life cycle of the jsp page is given below:

1. jspInit(): This method is the called form the init() method.
This method is of interface javax.servlet.jsp.JspPage.
We can override this method. This method will be called once when the container loads the servlet for the first time.
2. _jspService(): This method is called from the servlet's service method.
The container passes the Request and Response objects to this method.
We can't override this method. It is a method of javax.servlet.jsp.HttpJspPage interface.

3. jspDestroy: This method is called by the servlet's destroy() method.
We can override this method. It is a method of javax.servlet.jsp.JspPage interface.

<html>
<head>
<title>The lifecycle of jsp page</title>
</head>


<body>
<h1>Showing the life cycle of jsp using jspInit and jspDestroy</h1>
<%!
int num;
int counter;
public void jspInit()
{
counter++;
num = 10;
}


public void jspDestroy()
{
counter--;
num = 0;
}
%>


<%
out.println("The number is " + num + "<br>");
out.println("The counter is " + counter + "<br>");
%>
</body>
</html>

No comments:

Post a Comment

subversion video