0% found this document useful (0 votes)
8 views65 pages

Slot 6 - JSP

xcvcxvxcv

Uploaded by

Phuc Vinh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views65 pages

Slot 6 - JSP

xcvcxvxcv

Uploaded by

Phuc Vinh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 65

ĐẠI HỌC FPT CẦN THƠ

JavaServer Pages
Objective

Explain the need of JSP

List the benefits of JSP

Identify the situations where to use JSP and servlets

Explain JSP architecture

Identify various phases in the life cycle of a JSP page

Explain the various scriptlet elements in JSP

List and explain the use of various directives in JSP

JSP_Lương Hoàng Hướng 2


Objective

Explain the concept of implicit objects in JSP

List various types of implicit objects in JSP

Explain how to use the request object

Explain how to use the response object

Explain how to use the out object

Explain how to use the session object

JSP_Lương Hoàng Hướng 3


Objective

Explain how to use the application object

Explain how to use the pageContext object

Explain how to use the page object

Explain how to use the config object

Explain how to use the exception object

JSP_Lương Hoàng Hướng 4


Introduction

Scenario: A developer has to greet the user on his/her successful


registration on the Web page.

❖ Solution:
❑ Can be accomplished using Servlet technology.
❑ Create a Servlet class named GreetingServlet.
❑ Processes the request and generates a response to be
sent to the client.

JSP_Lương Hoàng Hướng 5


Introduction

❖ Figure shows the GreetingServlet class.

JSP_Lương Hoàng Hướng 6


Introduction
❖ In the code:
❑ To generate the output, the GreetingServlet code uses Java code
that will display the string value in the output.
❑ HTML elements embedded in the same Servlet that take care of the
presentation of the string on the Web page.
❑ Embedding HTML elements along with the Java code result in the
development of rigid applications, which have a tight coupling between
presentation and application layer.
❑ Any modifications in the Servlet results in page re-compilation, which
means embedded HTML elements are also re-compiled.

To segregate the presentation text from the application, Sun


laid a new specification referred to as JavaServer Pages
(JSP).

JSP_Lương Hoàng Hướng 7


JSP Page
❖ The JSP specification is an extension of the existing Servlet API and leverages
all the features of Servlets.
❖ JSP page:
❑ A text document containing static content, JSP tags, and Java code
which simplifies the generation of dynamic contents.
❑ Static contents are expressed as HTML, XML, or XHTML markup tags and
are used to generate the user interfaces on the page.
❑ Java code and JSP elements are used to generate dynamic contents on
the Web page.

JSP_Lương Hoàng Hướng 8


Application Layers
❖ Figure shows the application layers in the Web application.

JSP is used in the presentation


layer for presenting dynamic
content on the Web page.

❖ Most software applications consist of following three layers:


❑ Presentation layer contains user interface and code to build the interface.
❑ Application or Business layer contains code to validate data accepted in
presentation logic and other task specific code.
❑ Data access layer contains code to operate on the databases.
JSP_Lương Hoàng Hướng 9
Benefits of JSP

Separation of Content Generation from Presentation

• JSP separates content generation from presentation by implementing presentation


logic in a JSP page and content logic on server in a JavaBean component.

Emphasizing Reusable Components

• JSP pages use reusable components, such as JavaBeans which can be used by
multiple programs.

Simplified Page Development

• JSP allows a Web developer with limited knowledge in scripting language to design a
page.

Access and Instantiate JavaBean Components

• JSP supports the use of JavaBean components with JSP language elements. The user
can easily create and initialize beans and get and set their values.
JSP_Lương Hoàng Hướng 10
Use of Servlet and JSP

• JSP technology simplifies the process of


JSP creating pages by separating the Web
presentation from Web content.

• Servlets are well suited for handling binary


Servlet data dynamically, for example, for uploading
files or for creating dynamic images.

JSP_Lương Hoàng Hướng 11


Architecture of JSP

❖ Figure depicts JSP architecture.

JSP_Lương Hoàng Hướng 12


Life Cycle phases of JSP

Translation Phase Execution Phase

❖ In this phase, a servlet code to implement JSP tags is automatically generated,


compiled, and loaded into the Servlet container.
❖ The JSP engine writes the entire static contents such as markup tags to the response
stream in the Servlet.
❖ The JSP elements are converted into the equivalent Java code in the generated Servlet
code.
❖ After translation, the Servlet source code is compiled to .class file which is a
compiled Servlet class.
❖ The generated file is handed to the Servlet container for managing the Servlet life cycle
methods.

JSP_Lương Hoàng Hướng 13


Life Cycle phases of JSP

Translation Phase Execution Phase

❖ The generated Servlet class must implement the


javax.servlet.jsp.HttpJspPage interface defined in the JSP API.
❖ The HttpJspPage interface defines the life cycle methods.
❖ Following life cycle methods executed by the Servlet container on the loaded instance
of the converted Servlet are as follows:
jspInit() _jspService() jspDestroy()
• Similar to init() • Similar to • Executed by the Servlet
method of Servlet and is service() method container before
invoked on the of Servlet and is invoked destroying the JSP page
instantiated Servlet to by the Servlet container from the memory.
initialize it with the at each request.
parameters. • Is defined automatically
• A JSP page can override by the JSP container and
this method by including corresponds to the body
a definition for it in a of the JSP page.
declaration element.

JSP_Lương Hoàng Hướng 14


Element of JSP Page

❖ Contains tags that are categorized as Standard Tags and Custom Tags.

• They are used to process request, include Java objects and


directives, and perform actions on the JSP page.
• These tags include scripting tags, directive tags, and action tags.
Standard • All the tags are written within angular brackets <>.
Tags

• These tags perform operations, such as processing forms,


accessing databases, and other enterprise services, such as e-mail
and directories.
Custom Tags • These are created and loaded in separate libraries.

JSP_Lương Hoàng Hướng 15


Scripting Tags
❖ The JSP tags are used to embed Java code snippets in the JSP page for
generating dynamic contents on the Web page. These tags are as follows:
JSP Expressions
❖ Can be used to display individual variables or the result of some calculation
on a Web page.
❖ Contains a Java statement whose value will be evaluated and inserted into
the generated Web page.
❖ Is evaluated and the result is converted into a string. This evaluated string will
be displayed on the Web page.
❖ Syntax:
<%= Java Expression %>
where,
❑ <% = is the opening delimiter for the scriptlet.
❑ Java Expression indicates a valid Java statement.
❑ %> is the closing delimiter.

JSP_Lương Hoàng Hướng 16


Scripting Tags

JSP Scriptlets
❖ Is used to embed Java code within an HTML code.
❖ Is inserted into the _jspService() method of the servlet.
❖ Are executed when the request of the client is being processed.
❖ Are used to add complex data to an HTML form.
❖ Syntax:
<% Java code fragment %>
❖ Figure depicts the use of scriptlet in a JSP page.

JSP_Lương Hoàng Hướng 17


Scripting Tags

JSP Declarations
❖ Allow the user to define variables and methods that are inserted into the
Servlet, outside the service() method.
❖ Can declare variables within the scriptlet tag also, but that variable will be
local to that scriptlet.
❖ Variable declared using the declaration tag will be visible in the whole JSP
page.
❖ Cannot be declared within the scriptlet tag.
❖ Can be used to define multiple variables of same data type.

❖ Syntax:
<%! Java declaration code %>

JSP_Lương Hoàng Hướng 18


Scripting Tags
❖ The code snippet demonstrates the use of declaration tag in the JSP page
along with its output.

JSP_Lương Hoàng Hướng 19


Scripting Tags

Comments
❖ JSP comments are used for documenting JSP code which should not be visible
to the client when viewing the source of the Web page.
❖ These comments are called server-side comments and are encoded within
<%-- and --%> symbol.
❖ Syntax:

<%-- a JSP comment --%>

❖ Example: <%-- This is the JSP comment in a file --%>

JSP_Lương Hoàng Hướng 20


Directive Tags
❖ JSP directives control the processing of the entire page.
❖ These directives identify the packages to be imported and the interfaces to
be implemented.
❖ These directives do not produce any output. They inform the JSP engine
about the actions to be performed on the JSP page.
❖ These directives affect the overall structure of the Servlet class.
❖ Syntax:
<%@ directiveName attribute = "value"%>
where,
❑ directiveName is the name of the directive.
❑ attribute is attribute of directive.
❑ value is the value of attribute specified.

User can declare these directives anywhere on the page. However, mostly
they are declared at the top of the JSP page.
JSP_Lương Hoàng Hướng 21
Directive Tags

❖ In JSP, there are three directives:

• The page directive provides instructions that control the structure of


page the page

• The include directive includes static contents on the current JSP page
include

• The taglib directive includes the custom library tags on the JSP page
taglib

JSP_Lương Hoàng Hướng 22


page Directive
❖ Can be used to import classes from the packages, set the content type for the
page, enable or disable page as error page, set the language on the page, and
so on.
❖ Defines and manipulates a number of important attributes that affect the
entire JSP page.
❖ Is written at the beginning of a JSP page.
❖ Can contain any number of page directives.
❖ All directives in the page are processed together during translation and result
is applied together to the JSP page.
Example:

<%@ page import="java.util.*,java.io.* %>

❖ Used to import more than one package in the JSP page. If more than one
package is imported, then separate the package names by commas.

JSP_Lương Hoàng Hướng 23


page Directive
❖ Some of the attributes that can be defined for a page are as follows:

Language: Used to specify the language of the script. The default value of
language in a JSP page is Java.

errorPage: A path name to this JSP file sends exceptions. If the pathname
begins with a ‘/’, the path is relative to the root directory and is resolved by
the Web server. If not, the path name is relative to the current JSP file.

autoFlush: The buffered output is flushed automatically when the buffer is


full. If the autoFlush is set to true, the buffer will be flushed. Otherwise,
an exception will be thrown when the buffer overflows.

Session: The client must join an HTTP session in order to use the JSP page. If the
value is true, the session object refers to the current or new session. The default
value for session is true.

JSP_Lương Hoàng Hướng 24


page Directive
❖ Syntax:
<%@ page attribute %>
where,
❑ page indicates page directive.
❑ attribute specifies attribute-value pair of page directive.

❖ The valid parameters are as follows:

JSP_Lương Hoàng Hướng 25


page Directive
❖ The code snippet demonstrates how to set the language on the JSP page by
using the page directive.

JSP_Lương Hoàng Hướng 26


Include Directive
❖ Is used to insert content of another resource in a JSP page at run time.
❖ Content are included physically during page translation:
❑ The resource or content can be a file in a text-based format, such as text,
HTML, or JSP.
❑ The remote resource should be given with the proper relative file path in the
include directive.
❖ Syntax:
<%@ include file = "Filename" %>

where,
❑include indicates the directive and Filename specifies the name of
a file to include along with relative path.

JSP_Lương Hoàng Hướng 27


Include Directive

❖ Figure depicts the include directive.

JSP_Lương Hoàng Hướng 28


taglib Directive
❖ It allows the JSP page to create custom tags, which are defined by the user.
❖ A tag library is a group of custom tags that extend the functionality of a JSP page
one after the other.
❖ The taglib directive specifies name of the tag library, which contains
compiled Java code for a tag to be used.
❖ Using this tag library, the JSP engine determines what action is to be taken when a
particular tag appears in a JSP page.
❖ Syntax:

<%@ taglib uri="tagLibraryURI" prefix="tagPrefix" %>

where,
❑ tagLibraryURI specifies Uniform Resource Identifier (URI) that
identifies the tag library descriptor.
❑ prefix specifies tag name that is used to define a custom tag.
❑ tagPrefix defines the prefix string in prefix:tagname.

JSP_Lương Hoàng Hướng 29


taglib Directive

❖ Figure depicts the taglib directive.

JSP_Lương Hoàng Hướng 30


Implicit Object

❖ Implicit objects:
❑ Are a set of Java objects that are available in every JSP page.
❑ Are created and loaded by the Web container.
❑ Are referred to as pre-defined variables.
❑ Are accessible within the scripting elements in the JSP page.

❖ Where are implicit objects created?


❑ When the JSP page is translated into a Servlet class, all the implicit objects
declarations are taken within the _jspService() method.

JSP_Lương Hoàng Hướng 31


Types of Implicit Objects
❖ Some of the implicit objects provided by JSP are classified into
following categories:
❑ Input and Output Objects
❑ Scope Communication and Control Objects
❑ Servlet Objects
❑ Error Objects

Input and Output Objects


• These include objects such as request, response, and out.
• The request object controls the data coming into the page.
• The response object controls the information generated as a
result of request object.
• The out object controls the output data generated as a response to
the request.

JSP_Lương Hoàng Hướng 32


Types of Implicit Objects

Scope Communication and Control Objects

• The scope communication objects provide access to all the objects available in the given
scope.
• Objects in a JSP application are accessible according to the specified scope.
• The scope of an object is the section where that object is accessible

Servlet Objects

• These objects provide information about the page context.


• These processes request object from a client and sends the response object back to the
client.

Error Objects

• The error object handles error in a JSP page using an implicit object known as Exception.
• User can access this object by declaring JSP page as an error page.
• To do so, use the isErrorPage attribute of the page directive. For example, <%@page
isErrorPage="true" %>.

JSP_Lương Hoàng Hướng 33


Input and Output Objects

request Object

❑ It contains information sent from the client browser through HTTP


protocol.

❑ The information stored in the request object includes source of the


request, requested URL headers, cookies, and parameters associated with
the request.

❑ When the JSP page is translated into the Servlet class, the request object
is passed as a reference of the
javax.servlet.http.HttpServletRequest interface by the
container.

❑ The scope of the request object is page-level which means that the object
will be accessible only in the current page.
JSP_Lương Hoàng Hướng 34
Input and Output Objects
Table lists some of the methods of the request object available on the JSP page for
the developers.
Method Description
java.lang.String This method returns the value of a request parameter as a
getParameter(java.lang.String name) string, or null if the parameter does not exist.
java.lang.String[] This method returns an array of string objects which
getParameterValues(java.lang.String contains all the values the given request parameter has, or
name) null if the parameter does not exist.
java.util.Enumeration This method returns an enumeration of string objects, which
getParameterNames() contains the names of the parameters. If no parameters,
this method returns an empty enumeration.
void setAttribute(java.lang.String This method stores an attribute in the request. Attributes
name,java.lang.Object o) are reset between requests.

java.lang.Object This method returns the name of the string from the named
getAttribute(java.lang.String name) attribute.
java.lang.String getRemoteHost() This method returns the name of the client or the last proxy
that sends the request. If the hostname is not resolved, this
method returns the dotted-string form of the IP address.
java.lang.String getRemoteAddr() This method returns the Internet protocol address of the
client or last proxy that sent the request.

JSP_Lương Hoàng Hướng 35


Input and Output Objects
The code snippet demonstrates the methods of request object to receive
data from a registration page.

JSP_Lương Hoàng Hướng 36


Input and Output Objects

JSP_Lương Hoàng Hướng 37


Input and Output Objects
response Object
❑ Manages the response generated by JSP and uses HTTP protocol to send the
response to client.
❑ Is a reference of the javax.servlet.http.HttpServletResponse
interface.
Table lists the methods of response object to be used in the JSP page.
Method Description
public void addCookie(Cookie The method adds the specified cookie to the
cookie) response. This method can be called more than once
to set more than one cookies.
public void The method sends a temporary redirect response to
sendRedirect(java.lang.String the client using the specified redirect location URL.
location)throws This method can also accept relative URLs.
java.io.IOException)
public java.lang.String The method is used to encode the specified URL by
encodeURL(java.lang.String url) including session id. This form of the url can be used in
html tags that use a url, such as <a href………>. This
enables the server to keep track of the session.
public java.lang.String The method returns a rewritten url that can be used
encodeRedirectUrl(java.lang.String with the sendRedirect method of response
url) object.

JSP_Lương Hoàng Hướng 38


Input and Output Objects

out Object

❑ Represents the output stream, which will be sent to the client as a


response for the request.

❑ Is an instance of the javax.servlet.jsp.JspWriter class.

❑ Uses all standard write(), print(), and println() methods


defined in javax.servlet.jsp.JspWriter class to display the
output.

❑ Has page scope.

JSP_Lương Hoàng Hướng 39


Input and Output Objects

❖ Some of the methods of out object are as follows:


❑ void flush()throws java.io.IOException
 Flushes the buffer.
 Only the invocation of flush() will flush all the buffers in a chain of writers
and output streams.
 May be invoked indirectly if the buffer size is more.
The code snippet show how to flush the page content.
out.flush();
<jsp:forward page="login.jsp"/>

void clear()throws java.io.IOException


 Clears the contents of the buffer.
 If the buffer has been already been flushed, then the clear operation will
throw an IOException.

JSP_Lương Hoàng Hướng 40


Input and Output Objects
The code snippet shows how to clear the buffer.
// If buffer is not empty, buffer is cleared
if (out.getBufferSize() != 0)
out.clear();
❑ void close()throws java.io.IOException - For the
JspWriter, this method need not be invoked explicitly as the code
generated by the JSP container will automatically include close()
The code snippet shows the use of close() method.
out.print("Welcome!!!");
out.close();

❑ void println(java.lang.String x)throws


java.io.IOException - This method prints a string and then
terminates the line. For example, out.println("The output
is:" + ex);
JSP_Lương Hoàng Hướng 41
Input and Output Objects
❑ void print(java.lang.Strings)throws
java.io.IOException - This method prints a string. If the
argument is null, then the string ‘null’ is printed.
The code snippet demonstrates the use of print() method.
out.print("Welcome!!!");
out.print("To FPT Training");

JSP_Lương Hoàng Hướng 42


Scope Communication Objects

❖ The scope communication objects include:

session application pageContext


Object Object Object

❖ Session Object:
❑ Is an instance of javax.servlet.http.HttpSession interface.
❑ Has session scope.

❖ Some of the methods of session object are:


❑ public boolean isNew() - The isNew() method is used for checking
whether the session is newly created in this page or not. It will return false, if a
session already exists and true, otherwise.

JSP_Lương Hoàng Hướng 43


Scope Communication Objects

❑ public void setAttribute(java.lang.String


name,java.lang.Object value) - This method holds the
objects in the existing session.

The code snippet demonstrates how to associate a new session object with
the request.

JSP_Lương Hoàng Hướng 44


Scope Communication Objects
❑ public java.lang.Object
getAttribute(java.lang.String name) - The method
returns the object bound with the specified name in the current session,
otherwise returns null.
The code snippet shows how to access the named session object.

JSP_Lương Hoàng Hướng 45


Scope Communication Objects
❑ public java.util.Enumeration getAttributeNames() -
The method returns an enumeration of string objects. The string contains
the names of all the objects bound to this session.

❑ public void invalidate() - The method invalidates a session


and then, releases the objects bound to it.
The code snippet shows how to invalidate a session.
<%

❑ public void removeAttribute(java.lang.String


name)- The method removes the object bounded with the specified
name from this session. For example,
session.removeAttribute("loginuser");
JSP_Lương Hoàng Hướng 46
Scope Communication Objects
❖ application Object:
❑ Is used to share the data between all the application pages.
❑ Can be accessed by any JSP present in the application.
❑ Is an instance of the javax.servlet.ServletContext interface.
❑ Has application scope.
Some of the methods of application object are:
 public void setAttribute(java.lang.String name,
java.lang.Object object) - The method returns the servlet
container attribute with the given name or null, if there is no
attribute.
The code snippet shows how to set an attribute to be accessed in any
JSP page in the application.

JSP_Lương Hoàng Hướng 47


Scope Communication Objects
❑ public java.lang.Object
getAttribute(java.lang.String name) - The method returns
the servlet container attribute with the given name or null, if there is no
attribute.
❑ public java.util.Enumeration getAttributeNames() -
The method returns an enumeration containing the attribute names
available within this servlet context.

JSP_Lương Hoàng Hướng 48


Scope Communication Objects
The code snippet demonstrates how to retrieve all application objects set in
the application in the JSP page.

JSP_Lương Hoàng Hướng 49


Scope Communication Objects
❑ public void removeAttribute(java.lang.String
name)- This method removes the attribute with the given name from the
servlet context. For example,
application.removeAttribute("password");

❑ public java.lang.String getServerInfo() - This method


returns the name and version of the servlet container on which the servlet
is running. For example, out.println("Server
Information:"+ application.getServerInfo());

❑ public void log(java.lang.String msg) - This method


writes the specified message to a servlet log file, usually an event log. For
example, application.log (Hello log Message);

JSP_Lương Hoàng Hướng 50


Scope Communication Objects
❖ pageContext Object:
❑ Allows user to access all the implicit objects defined in the page scope.
❑ Is an instance of the javax.servlet.jsp.PageContext class.
❑ Uses methods defined in the PageContext class to access implicit objects in a
Web page.
❖ PageContext class provides following fields that are used by
pageContext object to find the scope or specify the scope of the objects:
• Specifies the scope for attributes stored in the
PAGE_SCOPE pageContext object. This is the default.

REQUEST_SCOPE • Specifies the request scope for attributes that


remain available until current request is complete.

SESSION_SCOPE • Specifies the scope for attributes stored in the


session object.
• Specifies the scope for attributes stored in the
APPLICATION_SCOPE application object that remains available
until it is reclaimed.
JSP_Lương Hoàng Hướng 51
Scope Communication Objects
❖ The pageContext object provides the following methods to
access all the attributes defined by implicit object in a page:
❑ void setAttribute(java.lang.String name,
java.lang.Object value, int scope) - This method registers
the name and value specified with appropriate scope. If the value passed
in is null, this method functions in the same way as
removeAttribute(name, scope).

❑ java.lang.Object getAttribute(java.lang.String name) - This method


returns the object associated with the name in the page scope, otherwise
it returns null. It throws java.lang.NullPointerException, if the
name is null.

JSP_Lương Hoàng Hướng 52


Scope Communication Objects

The code snippet shows the pageContext object used to set and retrieve
the object.

❑ public abstract java.util.Enumeration


getAttributeNamesInScope(int scope) - This method
enumerates all the attributes in a given scope. It throws
java.lang.IllegalArgumentException if the scope is invalid.

JSP_Lương Hoàng Hướng 53


Scope Communication Objects

The code snippet demonstrates how to retrieve all the objects with request
scope.

❑ public abstract void


removeAttribute(java.lang.String name) - This method
removes the object reference associated with the given name from all the
scopes. It throws java.lang.NullPointerException, if the
name is null.

JSP_Lương Hoàng Hướng 54


Scope Communication Objects

The code snippet demonstrates how to remove the objects from the JSP
page.

JSP_Lương Hoàng Hướng 55


Servlet Objects
❖ Include objects:

config
page Object
Object

❖ page Object:
❑ It is an instance of the java.lang.Object.
❑ It is an instance of the servlet processing the current request in a JSP page.
❑ It has page scope.

JSP_Lương Hoàng Hướng 56


Servlet Objects

The code snippet demonstrates how to access the methods of the Servlet
through page object.
<%
out.println(this.getServletInfo());
out.println(((Servlet)page).getServletInfo());
%>

❖ config Object:
❑ Stores the information of the servlet, which is created during the
compilation of a JSP page.
❑ Is an instance of the javax.servlet.ServletConfig interface.
❑ Provides methods to retrieve servlet initialization parameters.
❑ Represents the configuration of the servlet data where a JSP page is
complied.
❑ Has page scope.

JSP_Lương Hoàng Hướng 57


Servlet Objects
❑ Some of the methods of config object are:
public java.lang.String getInitParameter(java.lang.String name)

❑ This method returns a string which contains the value of the named
initialization parameter, otherwise it returns null.

public java.util.Enumeration getInitParameterNames()

❑ Returns the names of the servlet's initialization parameters as an


enumeration of string objects.

JSP_Lương Hoàng Hướng 58


Servlet Objects
❑ Code Snippet first retrieves all the init parameters
getInitParameterNames() and then, prints each parameter name
using getInitParameter().

JSP_Lương Hoàng Hướng 59


Servlet Objects

public ServletContext getServletContext()

❑ This method returns a reference to the servlet context.


❑ The code snippet given shows how to set a JavaBean object in the
ServletContext.
ServletContext context =
config.getServletContext();
context.setAttribute("dbBean", dbBean);

public java.lang.String getServletName()

❑ This method returns the name of this servlet instance.


❑ It can get the name that may be provided through the server
administration or assigned in the Web application deployment
descriptor.
❑ For example, String servletName =
config.getServletName();

JSP_Lương Hoàng Hướng 60


exception Object
❖ Runtime errors can be processed using the exception object
present in the JSP page.
❖ The exception object:
❑ Is used to trace the exception thrown during execution.

❑ Is available to the JSP page that is assigned as an error page.


❑ Is the instance of java.lang.Throwable class.

❑ Has page scope.

❖ Some of the methods of exception object are:


public String getMessage()
• Returns the descriptive error message associated with the
exception when it was thrown.

public void printStackTrace(PrintWriters)


• Prints the execution stack in effect when the exception was thrown
to the designated output stream.

JSP_Lương Hoàng Hướng 61


exception Object

public String toString()


• Returns a string combining the class name of the exception with its
error message.
❑ Code Snippet shown demonstrates how to handle exceptions in the JSP.

 The isErrorPage attribute is set to true, which indicates that the page is an error
page. The JSP expression (<%= %>) is used to create an instance of the exception
object.
 The out object is passed as an argument to the printStackTrace()method which
will display the stack trace information.
JSP_Lương Hoàng Hướng 62
Summary

JSP technology is used for developing dynamic Web sites and provides server-side scripting support for creating
Web applications.

A JSP page is a mixture of standard HTML tags, Web page content, and dynamic

content that is specified using JSP elements.

The JSP elements are expressions, scriptlets, comments, declarations, directives, and actions.

Directives are used to specify the structure of the resulting servlet and actions are JSP tags that transfer
control to other server objects or perform operations on other objects.

The page directive is used to set the page attributes, such as the language to be used and encoding format.

The include directive is used to insert a file referenced in the directive into the JSP page.

JSP_Lương Hoàng Hướng 63


Summary

JSP implicit objects are a set of Java objects that are created and maintained
automatically by the Web container.

The request object represents the request from the client for a Web page.

The response implicit object handles the response generated by JSP and sends
response to the client.

The out object represents the output stream.

The Web server creates a session object for multiple requests sent by a single user.
The application object represents the application to which the JSP page belongs.

JSP_Lương Hoàng Hướng 64


Summary

The pageContext object allows user to access all the implicit objects defined
in

the page scope.

The page object provides access to all the objects, which are defined
in a Web page.

The config object stores the information of the servlet.

When an error occurs, the execution of a JSP page is terminated. The


exception implicit object is used to trace exceptions that occur in a JSP page.

JSP_Lương Hoàng Hướng 65

You might also like