Professional Documents
Culture Documents
The life cycle of a JSP page can be split into four phases:
translation,
initialization,
execution, &
finalization.
Translation
i. When a request is first made for a JSP page, the
JSP engine (jasper for Tomcat) examines the JSP
file for its correct format & syntax.
ii. If the syntax check is successful, the JSP engine
translates the JSP page into its page
implementation class, which takes the form of a
standard Java servlet.
iii. After the pages implementation servlet has been
created, it will be compiled into a class file by the
JSP engine and will be ready for use.
iv. Each time a container receives a request, it first
checks whether the JSP file has changed since it
was last translated. If it has, its retranslated so
that the response is always generated by the most
up-to-date implementation of the JSP file.
Initialization
i. After the translation phase, the JSP engine loads the generated class file and create an instance of the servlet with the help of
servlet container (catalina for Tomcat) and the JSP page implementation servlet.
ii. A single instance of the servlet loaded into the memory is used to service all requests for the JSP page.
iii. After the JSP implementation servlet class is loaded, the container initializes the servlet instance with a call to an initialization
method jspInit().
Execution
i. Once the JSP implementation servlet has been initialized, the web container makes a separately threaded call to the
_jspService() method for each request to the JSP page.
ii. The _jspService() method provides all the functionality for handling a request & returning a response to the client.
Note: All the scriptlets and expressions end up inside this method, in the order in which they were declared inside the JSP page. Notice that JSP
declarations & directives arent included inside this method because they apply to the entire page, not just to a single request, and therefore
exist outside the method. The _jspService() method may not be overridden in the JSP page.
Finalization
i. When the page implementation servlet is about to be destroyed because of reasons, such as the server being low on memory and
wanting to free up some resources, or most commonly, the servlet container is shutting down or being restarted, The servlet
container calls the method jspDestroy().
ii. After this method has been called, the servlet can no longer serve any requests.
iii. This method can be implemented via a JSP method declaration, to release or close application-level resources (opened inside
the jspInit() method) when the servlet is shutting down.
By @mahesha999
v. The translation is done for only first request. Later, for each request , the JSP container checks if the JSP file has been changed
since it was last translated. If it has, then only it is retranslated.
vi. The user requesting a JSP page may notice a slight delay, since the translation phase takes a bit of time. This can be avoided by
pre-compiling JSP pages.
vii. After the translation, the JSP container loads the generated class file and create an instance of the servlet with the help of servlet
container (catalina for Tomcat).
viii. Then, the container initializes the servlet instance with a call to an initialization method jspInit().
ix. Once the JSP implementation servlet has been initialized, the web container makes a separately threaded call to the
_jspService() method for each request to the JSP page.
x. This method provides all the functionality for handling a request & returning a response to the client.
xi. When the page implementation servlet is about to be destroyed because of reasons, such as the server being low on memory and
wanting to free up some resources, or most commonly, the servlet container is shutting down or being restarted, The servlet
container calls the method jspDestroy().
xii. After this method has been called, the servlet can no longer serve any requests.
JSP ELEMENTS
There are three types of JSP elements we can use:
directive,
action, &
scripting.
Directives
i. Directives are instructions to the JSP container regarding page properties, importing tag libraries, and including content within
a JSP that remains the same between requests. Because directives are instructions rather than in-out processes, they cannot
produce any output via the out stream.
ii. There are three types of directives:
page directives
include directives
taglib directives
iii. All three directive types must be declared between <%@ and %> directive delimiters and take the following form:
<%@ directive {attribute="value"}* %>
iv. All directives should be placed at the top of the JSP page; however, the include directive is an exception.
The page Directive
The page directive specifies attributes for the page, such as session tracking, error page, and buffering requirements. All the
attributes are optional, because the essential ones have default values, shown in bold.
<%@ page language="java"
extends="package.class"
import="package.class, package.*, ..."
session="true|false"
buffer="none|default|sizekb"
autoFlush="true|false"
isThreadSafe="true|false"
info="Sample JSP to show tags"
isErrorPage="true|false"
errorPage="ErrorPage.jsp"
contentType="TYPE|
By @mahesha999
TYPE; charset=CHARSET|
text/html; charset=ISO-8859-1"
pageEncoding="default"
isELIgnored="true|false"
deferredSyntaxAllowedAsLiteral="true|false"
trimDirectiveWhitespaces="true|false"
%>
It import the tag library, containing custom actions, that is used in the page
<%@ taglib {uri="/tagLibraryURI" | tagdir="/WEB-INF/tags/dirname"
prefix="tagPrefix" %>
prefix Indicates a uniquely identifiable string, which is used in the <prefix:tagname> declaration to identify the
Scripting Elements
i. JSP scripting elements allow us to add small pieces of code (typically Java code) in a JSP page, such as an if statement to
generate different HTML depending on a certain condition.
ii. Thus, it enables dynamic content generation.
iii. Like actions, they are also executed when the page is requested.
iv. Scripting elements can be classified into the following categories:
Comments
Declarations
Scriptlets
Expressions
Expression language expressionsd
Comments <%-- comment --%>
They are used for explaining any complicated logic & for some documenting wrok.
Syntax: <%-- This is a JSP comment --%>
Scriplets <% ... %>
It enclose Java code (on however many lines) that is evaluated within the generated servlets _jspService() method to
generate dynamic content
Syntax:
Example:
<%
<%
// Java code
%>
It is used to declare instance variables and methods in the JSP page, which will be available to subsequent scriptlets and
By @mahesha999
expressions and so on for reference. Since these variables & methods are instance members, they are placed in JSP page
implementation servlet outside the _jspService() method.
Syntax:
Example:
<%! declaration;
[declaration;]+...%>
The <jsp:useBean> tag checks for an instance of a bean of the given class and scope. If a bean of the specified class exists, it
references it with the id; otherwise it instantiates it. The bean is available within its scope with its id attribute.
Syntax:
Example:
<jsp:useBean id="name"
scope="page|request|session|application" typeSpec/>
where
<jsp:useBean id="date"
class="com.apress.projsp.DateFormatBea
n"/>
Gets a property value from a JavaBeans component and adds it to the response for inclusion in the page.
Syntax:
Example:
<jsp:getProperty name="name" property="propertyName" />
<jsp:setProperty>
Sets the property of the bean referenced by name using the value.
Syntax:
<jsp:setProperty name="anotherBeanName"
propertyExpression
/>
Example:
<jsp:setProperty name="beanName"
property="property" value="value"/>
<jsp:setProperty name="beanName"
property="*" />
<jsp:setProperty name="beanName"
property="property"
param="paramName"/>
<jsp:setProperty name="beanName"
property="property" />
value="propertyValue"
<jsp:include>
Includes the response from a servlet or JSP page during the request processing phase
Syntax:
Example:
<jsp:include page="relativeURL" [flush="true|false"] />
or
<jsp:include page="relativeURL" flush="true">
<jsp:param name="parameterName"
value="parameterValue"/>
</jsp:include>
<jsp:include page="includedPage">
<jsp:param name="userName"
value="Dan" />
</jsp:include>
where,
By @mahesha999
It forwards the client request to a static resource, which can be an HTML file, a JSP page, or a servlet class in the same context as
the page.
Syntax:
Example:
<jsp:forward page="relativeURL" />
or
<jsp:forward page="/pages/login.jsp">
<jsp:param name="userName" value="Dan" />
</jsp:forward>
It is used within the body of <jsp:forward>, <jsp:include>, & <jsp:plugin> to supply extra name-value parameter pairs.
<jsp:param name="parameterName" value="parameterValue" />
request
i. It is reference to an instance of the javax.servlet.http.HttpServletRequest interface.
ii. The request object provides access to all the available information about the user request (such as request parameters and
headers) & may be used in exactly the same way that the HttpServletRequest parameter is used in the service()
method of a servlet.
iii. Example:
<h2>Hello <%=request.getParameter("userName")%>, Have a nice day!</h2>
response
i. The response object is a reference to an instance of the javax.servlet.http.HttpServletResponse interface.
ii. It contains the methods for setting headers and the status code, and adding cookies. It also provides methods related to session
tracking.
out
i. It represents an instance of the javax.servlet.jsp.JspWriter class that can be used to write character data to the
response stream in a similar manner to that seen by the java.io.PrintWriter class.
ii. The methods provided by the JspWriter such as print() & println() can be used to write text to the body of the response
page
i. The implicit page object references an instance of the JSPs page implementation class and is declared of type Object.
ii. The page object is rarely used in scripting elements and simply serves as a link between the JSP page and its implementing
servlet.
application
The implicit application object provides a reference to the javax.servlet.ServletContext interface that represents the
entire web application.
ii. This object holds references to other objects that more than one user may require access to, such as a database connection pool
shared by all application users.
iii. It also contains log( ) methods you can use to write messages to the container's log file.
i.
pageContext
i. The pageContext variable contains a reference to an instance of the class named javax.servlet.jsp.PageContext.
ii. It provides methods for accessing references to all the other objects and attributes for holding data that is shared between
components in the same page.
By @mahesha999
session
i. The session variable allows you to store & retrieve the client's session data, managed by the server.
ii. It's assigned a reference to an instance of a class that implements the javax.servlet.http.HttpSession interface, which
provides access to session data as well as information about the session, such as when it was created and when a request for the
session was last received.
exception
i. It is a reference to an instance of class java.lang.Throwable.
ii. The exception object is available only in error pages and contains information about a runtime error.
config
i. The config object simply provides the JSP developer with access to the javax.servlet.ServletConfig object that is used
by the web container to configure the JSP page.
ii. The ServletConfig interface is most commonly used to provide access to any initialization parameters
Variable name
application
config
exception
out
page
pageContext
request
response
session
Java type
javax.servlet.ServletContext
javax.servlet.ServletConfig
java.lang.Throwable
javax.servlet.jsp.JspWriter
java.lang.Object
javax.servlet.jsp.PageContext
javax.servlet.http.HttpServletRequest
javax.servlet.http.HttpServletResponse
By @mahesha999
By @mahesha999