JSP's Life Cycle Can Be Grouped Into Following Phases
JSP's Life Cycle Can Be Grouped Into Following Phases
JSP's Life Cycle Can Be Grouped Into Following Phases
A java servlet file is generated from the JSP source file. This is the first step in its tedious
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.
The generated java servlet file is compiled into a java servlet class.
Note: The translation of a JSP source page into its implementation class can happen at any
time between initial deployment of the JSP page into the JSP container and the receipt and
processing of a client request for the target JSP page.
3. Class Loading:
The java servlet class that was compiled from the JSP source is loaded into the container.
4. Execution phase:
In the execution phase the container manages one or more instances of this class in response
to requests and other events. The interface JspPage contains jspInit() and jspDestroy(). The
JSP specification has provided a special interface HttpJspPage for JSP pages serving HTTP
requests and this interface contains _jspService().
5. Initialization:
jspInit() method is called immediately after the instance was created. It is called only once
during JSP life cycle.
6. _jspService() execution:
This method is called for every request of this JSP during its life cycle. This is where it
serves the purpose of creation. Oops! it has to pass through all the above steps to reach this
phase. It passes the request and the response objects. _jspService() cannot be overridden.
7. jspDestroy() execution:
This method is called when this JSP is destroyed. With this call the servlet serves its purpose
and submits itself to heaven (garbage collection). This is the end of jsp life cycle.
jspInit(), _jspService() and jspDestroy() are called the life cycle methods of the JSP.
JSP 1 c Answer
<html>
<body>
</body>
</html>
JSP 1d Answer
<html>
<body>
{
a[i]=a[i-1]+a[i-2];
out.println(a[i]);
}
%>
</body>
</html>
Jsp Expressions And Scriptlets
Syntax of JSP Expressions are:
Syntax of JSP Scriptles are with <%= and ends with %>. Between these this you can put
anything and that will converted to the String and that will be displayed.
Example:
<%="Hello World!" %>
Above code will display 'Hello World!'.
<%
//java codes
%>
JSP Scriptlets begins with <% and ends%> .We can embed any amount of java code in the
JSP Scriptlets. JSP Engine places these code in the _jspService()method.
Implicit objects in jsp are the objects that are created by the container automatically
and the container makes them available to the developers, the developer do not need to
create them explicitly. Since these objects are created automatically by the container and are
accessed using standard variables; hence, they are called implicit objects. The implicit
objects are parsed by the container and inserted into the generated servlet code. They are
available only within the jspService method and not in any declaration. Implicit objects are
used for different purposes. Our own methods (user defined methods) can't access them as
they are local to the service method and are created at the conversion time of a jsp into a
servlet. But we can pass them to our own method if we wish to use them locally in those
functions.
There are nine implicit objects. Here is the list of all the implicit objects:
Object Class
javax.servlet.ServletConte
application
xt
javax.servlet.ServletConfi
config
g
exception java.lang.Throwable
javax.servlet.jsp.JspWrite
out
r
page java.lang.Object
PageConte javax.servlet.jsp.PageCon
xt text
javax.servlet.ServletRequ
request
est
javax.servlet.ServletResp
response
onse
javax.servlet.http.HttpSes
session
sion
Application: These objects has an application scope. These objects are available at the
widest context level, that allows to share the same information between the JSP page's
servlet and any Web components with in the same application.
Out: This object allows us to access the servlet's output stream and has a page scope.
Out object is an instance of javax.servlet.jsp.JspWriter class. It provides the output
stream that enable access to the servlet's output stream.
Page: This object has a page scope and is an instance of the JSP page's servlet class
that processes the current request. Page object represents the current page that is used
to call the methods defined by the translated servlet class. First type cast the servlet
before accessing any method of the servlet through the page.
Request: Request object has a request scope that is used to access the HTTP request
data, and also provides a context to associate the request-specific data. Request object
implements javax.servlet.ServletRequest interface. It uses the getParameter() method
to access the request parameter. The container passes this object to the _jspService()
method.