You are on page 1of 56

BS Grupo SฺAฺCฺ

Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

Developing with JavaServer Pages


Technology

u se
to
B E ense
R NA le lic
B E rab
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
B ER ailฺc tude
RO hotm this S
a b e@
er n
a r b
g u il
(r a
BS Grupo SฺAฺCฺ

Objectives

After completing this lesson, you should be able to do the


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

following:
• Evaluate the role of JSP technology as a presentation
mechanism
• Author JSP pages
• Process data received from servlets in a JSP page
• Describe the use of tag libraries

u se
to
B E ense
R NA le lic
B E rab
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
B ER ailฺc tude
RO hotm this S
a b e@
er n
a r b
g u il
(r a

Developing Applications for the Java EE 6 Platform 5 - 2


BS Grupo SฺAฺCฺ

JSP Technology as a Presentation Mechanism

JSP pages are text-based documents that describe how to


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

process a request and create a response.


• Using JSP technology, a page designer creates a
document to generate dynamic content.
• JSP elements:
– Enable external object access
– Add canned programming capabilities
• Source files for JSP pages typically end with the .jsp
extension. u se
to
B E ense
R NA le lic
B E rab
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
R ilฺc ude
B E t
JSP Technology as
o tma his S
RO a hPresentation Mechanism
JSP technology has@ Write Once,tRun Anywhere™ properties. A JSP page is written using a
technology that aYou e
bis not proprietary; it can run on any web server and can be accessed from any
web browser.
e r n do not concern yourself with platform-specific issues when you create or
r b
la JSP technology. This feature also allows for a greater choice when you select
deploy
u i
gappropriate JSP technology tools and components. Furthermore, the components accessed
(ra or used within a JSP page, such as components that are based on the JavaBeans component
architecture (or beans), EJB components, and tag libraries, are all designed to be reusable
components.

Developing Applications for the Java EE 6 Platform 5 - 3


BS Grupo SฺAฺCฺ

JSP Technology as a Presentation Mechanism

JSP technology:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

• Uses beans to interact with server-side objects


• Uses tag libraries to develop and extend the canned
capabilities provided by actions
• Allows for a high degree of separation between the static
and dynamic content in a JSP page
• Provides (where necessary) a powerful scripting language
for JSP pages
• Is an integral part of the Java EE platform, and so provides u se
to
front-end access to EJB components BE nse A lice
N
B ER rable
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
B ER ailฺc tude
RO hotm this S
a b e@
er n
a r b
g u il
(r a

Developing Applications for the Java EE 6 Platform 5 - 4


BS Grupo SฺAฺCฺ

Presentation Using JSP Pages Compared to


Servlets
JSP pages are web components that are based on the servlet
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

model and that run as servlets:

Characteristic JSP Servlets

Request and response Same model


model
Use of markup language Yes, similar to HTML No, uses println
statements
Runtime Have similar runtime benefits over other
u se
scripting tools, such as CGI to
B E ense
R NA le lic
B E rab
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
B ER ailฺc tude
RO hotm this S
a b e@
er n
a r b
g u il
(r a

Developing Applications for the Java EE 6 Platform 5 - 5


BS Grupo SฺAฺCฺ

Presentation Using JSP Pages Compared To


Servlets
Characteristic JSP Servlets
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

Capabilities Similar to other dynamic Generated HTML


content technologies response is static text
created by println
statements.
Automatic recompilation Yes No

Developer skills JSP pages can be Servlets are authored by


authored by nonsoftware software developers.
developers.
u se
Debugging Quicker and easier to test More difficult to test to
layout and presentation layouts
B E ense
A lic
Ndebug
Harder to debug Easier R
E rablecompile-
to
B
Rtime problems and
eexceptions
L A n s f
G UI -tra runtime

S A non
Copyright © 2011,E S Uand/or itssaffiliates.
a eฺ reserved.
a Allidrights
O J om) h nt Gu
Oracle

T
B ER ailฺc tude
RO hotm this S
a b e@
er n
a r b
g u il
(r a

Developing Applications for the Java EE 6 Platform 5 - 6


BS Grupo SฺAฺCฺ

Worker Beans, JSTL, and Custom Tags

You can separate programmatic functionality from presentation


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

in JSP components in two ways:


• Incorporate classes with the <jsp:useBean> tag and the
JSTL.
– Useful for carrying data into the JSP component
– JSTL allows programmatic behavior without scriptlets
• Make use of custom tag libraries.
– Most valuable when tag libraries are general-purpose and
reusable u se
to
E ense
– Less useful for page-specific logic, such as unique form
B
processing NA lic
B ER rable
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
R ilฺc ude
B E
Worker Beans, JSTL,
R O and o t m a
Custom
i s St
Tags
Both useBean classesh
@ and custom th tags have a part to play in the development of web
n a be experience has shown that they are not sufficient on their own to
applications. However,
implement
r b erthe full functionality of the web tier without detriment to maintainability.
u i la
g
(ra

Developing Applications for the Java EE 6 Platform 5 - 7


BS Grupo SฺAฺCฺ

JSP Page Deployment Mechanism

JSP pages:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

• Are translated into servlets on demand


• Can be deployed in the same manner as an HTML page,
by copying the file onto the server

u se
to
B E ense
R NA le lic
B E rab
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
R ilฺc ude
B E t
JSP Page Deployment
o tma his S
RO hMechanism
As mentioned, JSP @ t
pages are translated into servlets. This translation must occur at some
e
nab it could be the developer’s responsibility to use a tool that is provided by
point before the JSP page is invoked. In principle, the translation could be done in various
r
ways. Foreexample,
a rb to do the translation before deployment.
theilvendor
a g u tools do exist, and are widely used. However, the Java EE specification states that a
(r Such
web container that supports JSP technology must be able to translate a JSP page on
demand.
JSP technology is designed to simplify presentation design, and must be manageable by
content authors, as well as by developers. The Java EE specification takes the approach that
a JSP page can be deployed in the same manner as an HTML page, by copying the file onto
the server in the appropriate place.

Developing Applications for the Java EE 6 Platform 5 - 8


BS Grupo SฺAฺCฺ

JSP Page Translation Procedure

JSP Container
JSP Page
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

2
1 Yes
Web Client
Transform
The JSP page was
Request not previously
accessed or has
Internet been modified

No Compile

Web Client u se
4 3 to
B E ense
R NA le lic
jsp_servlets
B E rab
I L AR nsfe
Web Client
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
R ilฺc ude
B E
Translate-on-Request
RO hProcess
o tma hofisJSP St Page
As mentioned, a JSP @ container mustt be able to transform a JSP page into a servlet, and then
compile and load e
b the servlet before executing the page. The JSP container does not need to
aprocess
go through e r n
this on every request. The JSP container recognizes when a JSP page
has r b
labeen updated since the last request, and recompiles pages as necessary. Automatic
u i
gpage recompilation provides a robust mechanism for maintaining pages that require frequent
(ra modification. The figure in the slide illustrates the page translation procedure for JSP pages.

Developing Applications for the Java EE 6 Platform 5 - 9


BS Grupo SฺAฺCฺ

Java Code Embedded in JSP Pages

Ideally, a JSP page should be concerned with presentation


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

logic only. A servlet is a better alternative for processing logic


and for flow control. Using scripting code for processing logic
and flow control within a JSP page results in problems that
include:
• A JSP page author:
– Must code well in the scripting language
– Might need more business domain knowledge
• It is more difficult to see presentation information when you u se
view the JSP page. to
B E ense
• Debugging is more difficult because of the added
R NA le lic
complexity and decreased clarity. BE rab
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
R ilฺc ude
B E
Java Code Embedded
RO hinotJSP ma Pages i s St
The JSP specification @ also defines ththe requirements for a scripting language that is used
n a beInstead of identifying a specific scripting language that must be used, the
within a JSP page.

r b er lists some basic capabilities that a scripting language for JSP technology must
specification

u i la If no scripting language is specified, the Java programming language is used by


provide.

( ragdefault.

Developing Applications for the Java EE 6 Platform 5 - 10


BS Grupo SฺAฺCฺ

Authoring JSP Pages

A JSP page contains:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

• Standard markup tags, such as HTML or XML


• Associated text data
• A variety of elements that are defined by the JSP
specification

u se
to
B E ense
R NA le lic
B E rab
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
B ER ailฺc tude
RO hotm this S
a b e@
er n
a r b
g u il
(r a

Developing Applications for the Java EE 6 Platform 5 - 11


BS Grupo SฺAฺCฺ

JSP Page Components

HTML/XML
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

Markup

Directives

Implicit Objects

While (it.hasNext)() {...

Scripting Elements
u se
to
B E ense
Actions
R NA le lic
B E rab
I L AR nsfe
A GU on-tra JSP Page

S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
R ilฺc ude
B E a t
JSP Page Components
RO hotm this S
The figure in the slide
@ shows the various components of a JSP page.
e
ab a subset of these components. Detailed coverage of JSP pages can be
This course covers
found in r n
e course titled Web Component Development with Servlet and JSP Technologies.
the
a r b
g u il
(ra

Developing Applications for the Java EE 6 Platform 5 - 12


BS Grupo SฺAฺCฺ

Syntactic Forms of JSP Tags

Syntactic forms of tags based on JSP tags can be represented


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

in two different ways:

Old Syntax: Similar to other XML Syntax: With beginning and


tag-based dynamic end tags
presentation technologies
<%! ... %> <jsp:declaration> ...
</jsp:declaration>
<%= ... %> <jsp:expression> ...
u se
</jsp:expression> to
B E ense
NA le lic
<% ... %> <jsp:scriptlet> ...
R
E rab
</jsp:scriptlet>
B
<%@ ... %> <jsp:directive.type
I L AR ns... fe />
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
R ilฺc ude
B E
Syntactic Forms of
ROJSP o ma is St
Tags
t
Syntactic forms of JSP@ htags can tbeh represented in two different ways:
The first style a
n bebeen part of the JSP specification from the beginning and is similar to other
has
tag-based
r b erdynamic presentation technologies, such as ASP.
u
Thei lasecond style is similar to XML, with beginning and ending tags. The table in the slide
g
(ra gives examples of the two styles of tags.
The JSP 2.1 specification specifically supports the XML syntax for tags. Some, but not all
prior implementations also support the XML syntax for tags.

Developing Applications for the Java EE 6 Platform 5 - 13


BS Grupo SฺAฺCฺ

JSP Technology Directives

• Contain information to help a JSP container configure and


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

run a JSP page


• Are associated with the compiled servlet that is created
from the JSP page
• Do not produce output
• Have the following generic syntax:
<%@ directive attribute="value" ... %>

u se
to
B E ense
R NA le lic
B E rab
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
R ilฺc ude
B E a t
RO hotm this S
JSP Technology Directives
Note: The out stream @ is one of several implicit objects that all JSP pages can access. The out
e
nab in depth in SL-314-EE6, Web Component Development with Servlet and
stream represents a stream that displays information to the client. Implicit objects and their
uses aree r
presented
JSPi l a rb
Technologies.
a g u
(r

Developing Applications for the Java EE 6 Platform 5 - 14


BS Grupo SฺAฺCฺ

JSP Directives

JSP Container
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

import=“java.util.*, bank.*”

page

JSP Page
file=“StatementHeader.jsp”

include

Compile

uri=“http://www.XXX”;prefix=“bank”
u se
to
taglib
B E ense
R NA le lic
Run B E rab
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
R ilฺc ude
B E a t
JSP Directives O
R hotm this S
The directive is the @ directive type and the attribute and value pair is one or more of the
ab e
applicable combinations for the defined directive type. Three directive types are defined in the
current JSPe r n
specification:
a r b
•il The page directive
g u
(ra • The include directive
• The taglib directive
The figure in the slide illustrates the three directive types.
Although directives are generally position-independent, directives that affect how a JSP
container handles a page are often specified on the first line of a JSP page for clarity.

Developing Applications for the Java EE 6 Platform 5 - 15


BS Grupo SฺAฺCฺ

The page Directive

The page directive defines page-dependent attributes:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

• An attribute and value pair cannot be redefined within a


translation unit, with the exception of the include page
directive.
• Redefining a page directive results in a fatal translation
error, unless the new and the old definitions are the same.

u se
to
B E ense
R NA le lic
B E rab
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
The page Directive B ER ailฺc tude
RO hotm this S
Refer to the JSP 2.1@ specification and syntax sheet for a complete listing of page directives,
attribute usage, e
absyntax, and default values.
e r n
Note: Abtranslation unit is defined as a JSP page source file and any files included through
a r
theilinclude directive.
a g u
(r

Developing Applications for the Java EE 6 Platform 5 - 16


BS Grupo SฺAฺCฺ

The page Directive

Examples using both styles of syntax:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

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

<%@ page buffer="5kb" autoFlush="false" %>

<jsp:directive.page errorPage="error.jsp" />

u se
to
B E ense
R NA le lic
B E rab
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
B ER ailฺc tude
RO hotm this S
a b e@
er n
a r b
g u il
(r a

Developing Applications for the Java EE 6 Platform 5 - 17


BS Grupo SฺAฺCฺ

JSP page Directives

language=“Java”
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

session=“false”

buffer=“none”

errorPage=“LoginError.jsp”
JSP
Page
import=“java.util.*,bank.*”

isErrorPage=“true”

isThreadSafe=“true”
u se
to
B E ense
NA le lic
info=“Banking App V1.1”

R
E rab
autoFlush=“false” B
I L AR nsfe
A GU on-tra
isELIgnored=“ignored”

S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
JSP page Directives B ER ailฺc tude
RO hotm this S
The following page @directive attributes are defined by the current JSP specification:
• The language
e
ab attribute indicates the scripting language used.
r n
esession attribute specifies whether the page participates in a session.
• The
a r b
g u•il The buffer attribute specifies the buffering model for the out stream.
(ra • The errorPage attribute defines a URL used to forward throwable objects that are not
handled by the page for error processing.
• The import attribute describes an import list of types that are available for use by the
scripting environment.
• The isErrorPage attribute indicates that the page is the target of another page’s
errorPage directive when set to true.
• The isThreadSafe attribute sets the level of thread safety implemented in the page.
• The info attribute contains a string included into the translated page that can contain
some information about the page implementation.
• The autoFlush attribute determines what happens when the output buffer is filled.
• The isELIgnored attribute determines whether EL expressions are ignored or
translated.

Developing Applications for the Java EE 6 Platform 5 - 18


BS Grupo SฺAฺCฺ

The include Directive

The include directive:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

• Inserts the text of the specified resource into the .jsp file
at page translation time
• Treats resources as static objects
• Can be other HTML files or other JSP pages that contain
text, or code, or both
Examples of the include directive:
<%@ include file="relativeURL" %> u se
to
B E ense
Or
R NA le lic
B E rab
<jsp:directive.include file="relativeURL"
A R s/> fe
I L n
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
The include Directive B ER ailฺc tude
RO hotm this S
Changes to included @resources may not be recognized by the JSP container and, therefore,
may not forcea b e
automatic recompilation of the including page.
e r n
a r b
g u il
(ra

Developing Applications for the Java EE 6 Platform 5 - 19


BS Grupo SฺAฺCฺ

The include Directive

<tr>
<td><b>Date</b></td>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

<td><b>Check</b></td>
<%@ include <td><b>Description</b></td>
file=“Stmthdr.html”%> <td><b>Amount</b></td>
JSP Container
<tr>

statement. include Stmthdr


jsp .html

Compile
u se
to
B E ense
R NA le lic
B E rab
I L AR nsfe
GU on-tra
jsp_servlets

S an A
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
R ilฺc ude
B E(continued)
The include Directive
R O otma is St
h
The figure in the slide illustrates
@ th use of an include directive to include static HTML
the

n a be
heading information.

r b er
u i la
g
(ra

Developing Applications for the Java EE 6 Platform 5 - 20


BS Grupo SฺAฺCฺ

Declarations, Expressions, and Scriptlets

Scripting elements allow a page designer to provide advanced


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

programming capabilities. Scripting elements include:


• Declarations
• Expressions
• Scriptlets

u se
to
B E ense
R NA le lic
B E rab
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
B ER ailฺc tude
RO hotm this S
a b e@
er n
a r b
g u il
(r a

Developing Applications for the Java EE 6 Platform 5 - 21


BS Grupo SฺAฺCฺ

Declarations, Expressions, and Scriptlets

Declarations Expressions Scriptlets


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

Use To declare instance- To retrieve values of To embed snippets


scope variables and page variables, of scripting code
methods methods, or bean
fields
Syntax requirement Code that must: Any legal Raw blocks of
• Conform to the expression in the program code that
scripting language scripting language use Java as the
syntax default scripting
• Form declarative language
statement
u se
Description Do not produce Are evaluated by • Are inserted into to
output into the the JSP container the generated B E ense
current out stream at runtime, with servlet without
R NA le lic
results converted to B E rab
modification

I L AR nsfe
a String object on • Use sparingly.

GU on-tra
the page

S an A
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
B ER ailฺc tude
RO hotm this S
a b e@
er n
a r b
g u il
(r a

Developing Applications for the Java EE 6 Platform 5 - 22


BS Grupo SฺAฺCฺ

Declaration Scripting Elements

Syntax Alternate Syntax


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

<%! <jsp:declaration>
declaration(s) declaration(s)
%> </jsp:declaration>

Example
<%! final String SHOWDETAILS_URL = "/showdetails.jsp";
boolean hasAccounts(Customer c) {return
!c.getAccounts().isEmpty();} %>

se
<jsp:declaration>
// This instance variable is assigned at initialization
to u
time
B E ense
protected BankMgr bankMgr = null;
R NA le lic
</jsp:declaration>
B E rab
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
B ER ailฺc tude
RO hotm this S
a b e@
er n
a r b
g u il
(r a

Developing Applications for the Java EE 6 Platform 5 - 23


BS Grupo SฺAฺCฺ

Expression Scripting Elements

Syntax Alternate Syntax


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

<%! <jsp:expression>
declaration(s) expression
%> </jsp:expression>

Example
<td> <%=acct.getBalance()%> </td>

Or

<td>
u se
<jsp:expression>
to
acct.getBalance()
B E ense
</jsp:expression>
R NA le lic
</td> B E rab
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
B ER ailฺc tude
RO hotm this S
a b e@
er n
a r b
g u il
(r a

Developing Applications for the Java EE 6 Platform 5 - 24


BS Grupo SฺAฺCฺ

Scriptlet Scripting Elements


Syntax Alternate Syntax
<% code_segment %> <jsp:scriptlet>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

code_segment
</jsp:scriptlet>

Example
1 <%
2 if(isAllowedTransaction() == false){url=ScreenMgr.BANK_ERRORPAGE; }
3 else {
4 Vector checkList = account.getCheckByAmount(amt);
5 Iterator it = checkList.iterator();
6 double totalCheckAmount = 0.00;
7 while (it.hasNext()) { u se
to
8 Check chk = (Check) it.next();
B E ense
NA le lic
9 totalCheckAmount += chk.amount();
10 } // end while R
E rab
11 } // end if/else B
12 %>
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
R ilฺc ude
B E a t
R hotm this S
Scriptlet ScriptingOElements
Use scriptlets sparingly.
@ Besides being difficult to read, the use of scriptlets does not support
the separationaof e
bweb-tier roles and code reusability. Custom tag libraries are an excellent
alternative e r
tonscriptlets.
a r b
g u il
(ra

Developing Applications for the Java EE 6 Platform 5 - 25


BS Grupo SฺAฺCฺ

Scriptlet Scripting Elements


Example
1 <%
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

2 Check chk;
3 while (it.hasNext()) {
4 chk = (Check)it.next();
5 // end of first code fragment
6 %>
7 <%-- output check amount using HTML --%>
8 <br> Check Amount: <%=chk.getAmount()%> </br>
9 <%
10 } // closing bracket for while loop
11 // end of second code fragment
12 %>

Translates to:
1 Check chk;
u se
to
2 while (it.hasNext()) {

E ense
3 chk = (Check)it.next();
4 // end of first code fragment
B
5 out.write("\t\t<br> Check Amount: ");
R NA le lic
6
7
out.print(chk.getAmount());
out.write(" </br>\r\n");
B E rab
8 out.write("");
I L AR nsfe
GU on-tra
9 } // closing bracket for while loop
10 // end of second code fragment

S an A
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
B ER ailฺc tude
RO hotm this S
a b e@
e rn
a r b
g u il
(r a

Developing Applications for the Java EE 6 Platform 5 - 26


BS Grupo SฺAฺCฺ

Thread-Safety Implications

Declarations occur at the instance level of the generated


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

servlet. Therefore:
• All requests to the JSP page share these variables and
methods
• Thread-safety problems can occur with this technique.
All of the cautions that apply to servlets and thread safety apply
to JSP page declarations.

u se
to
B E ense
R NA le lic
B E rab
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
B ER ailฺc tude
RO hotm this S
a b e@
er n
a r b
g u il
(r a

Developing Applications for the Java EE 6 Platform 5 - 27


BS Grupo SฺAฺCฺ

Processing Data from Servlets

The JSP specification defines a standard set of action types


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

that all JSP containers must implement, including:


• Create or use beans
• Set and get bean properties
• Include static and dynamic resources in the current page’s
context
You can define additional action types using custom tag
libraries.
u se
to
B E ense
R NA le lic
B E rab
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
R ilฺc ude
B E t
Processing Data from
RO hServlets
o tma his S
Server-side actions@ provide a cannedt range of operations to the JSP page developer. Actions
use an XML-style e
abatag format to allow a JSP page developer to use, modify, or create objects,
e r n
without requiring detailed understanding of the Java programming language. These objects
may r b
lainclude data that is passed from a servlet. Based on the specific action type, an action
u i
gmay affect the current output stream, that is, produce content.
(ra You can define additional action types as necessary using custom tag libraries that are
identified with the taglib directive.
The syntax for an action includes an XML-style action tag and an associated list of attribute
and value pairs. For example, the action syntax to create or use a bean within a JSP page is:
<jsp:useBean id="name" scope="scope" typeSpec />
A brief summary of the standard action types defined in the JSP 2.1 specification follows in
the subsequent slides.
Note: For a more complete explanation of the range of operations and syntax of each action,
refer to the JSP specification or to the course titled Web Component Development with
Servlet and JSP Technologies.

Developing Applications for the Java EE 6 Platform 5 - 28


BS Grupo SฺAฺCฺ

The jsp:useBean Action

• Creates or locates an existing bean that matches the


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

criteria in the tag


• Associates the bean instance with a scope and action ID
• Makes the ID accessible by scripting elements and custom
tags

u se
to
B E ense
R NA le lic
B E rab
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
B ER ailฺc tude
RO hotm this S
a b e@
er n
a r b
g u il
(r a

Developing Applications for the Java EE 6 Platform 5 - 29


BS Grupo SฺAฺCฺ

The jsp:useBean Action

Syntax of jsp:useBean:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

<jsp:useBean id="name" scope="scope" typeSpec />

Alternate syntax with initialization code:


<jsp:useBean id="name" scope="scope" typeSpec >
<% ...initialization code... %>
</jsp:useBean>

typeSpec can be any of the following: u se


e to
E ns
class="className"
N AB li c e
class="className" type="typeName"
B ER rable
beanName="beanName" type=" typeName"
I L AR nsfe
type="typeName" GU -tra A non
S
Copyright © 2011,E S Uand/or itssaffiliates.
a eฺ reserved.
a Allidrights
O J om) h nt Gu
Oracle

T
R ilฺc ude
The jsp:useBeanOAction BE m a
(continued) St
R o t i s
h the name variable has a specific value and the scope
h in thetslide,
In the first syntax example
variable is one of
@
bethe four valid JSP scopes, including page, request, session, or
n a
r The typeSpec attribute and value pair determines the specific steps taken to
r b e
application.

u i la or create a bean instance. The typeSpec parameter can be one of a number of legal
locate

( ragattribute and value pairs for this action that specify either the class or type of the object, a
combination of class and type, or combination of type and bean name.

Developing Applications for the Java EE 6 Platform 5 - 30


BS Grupo SฺAฺCฺ

The jsp:useBean Action


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

useBean
1

scope

2 <jsp:useBean id= “customer” scope=“session”


class= “bank.Customer”>

se
<% customer.init (pageContext.getSession())%>
class </jsp:useBean>
to u
3 B E ense
R NA le lic
B E rab
JSP Page
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
R ilฺc ude
The jsp:useBeanOAction BE m(continued)
a St
R o t i s
h
The figure in the slide illustrates
@ th jsp:useBean action.
the

n a be
r b er
u i la
g
(ra

Developing Applications for the Java EE 6 Platform 5 - 31


BS Grupo SฺAฺCฺ

The jsp:useBean Action

This graphic shows the id attribute.


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

id
attribute value <jsp:useBean id=“account” scope=“session”
Class=“bank.Account”/>
Session

<%
Vector checkList = account.getCheckByAmount (amt);
If (checkList != null) {
JSP
Iterator it = checkList.iterator();
Page
se
Double totalCheckAmount = 0.00;
While (it.hasNext()) {
...
to u
%>
B E ense
R NA le lic
B E rab
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
R ilฺc ude
The jsp:useBeanOAction BE m a
(continued) St
R o t i s
The id attribute in theh
@ jsp:useBean th action identifies the object to the JSP container and
n a be must be unique within its translation unit. In the Java programming
page. The id attribute
language,
b r becomes the name of a method-scope variable in the generated servlet. You
eid
can
i r
lause this name to access the object within a page from the scripting language. In addition,
u
g JSP page can access object instances that are created by jsp:useBean through the
the
(ra pageContext object.

Developing Applications for the Java EE 6 Platform 5 - 32


BS Grupo SฺAฺCฺ

The jsp:useBean Action

Examples of jsp:useBean:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

• Usage example for the id attribute:


<jsp:useBean id="account" class="bank.Account"/>
• To retrieve the account balance, use the expression:
<%=account.getBalance() %>

u se
to
B E ense
R NA le lic
B E rab
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
B ER ailฺc tude
RO hotm this S
a b e@
er n
a r b
g u il
(r a

Developing Applications for the Java EE 6 Platform 5 - 33


BS Grupo SฺAฺCฺ

The jsp:useBean Scopes

JSP Page JSP Pages JSP Page JSP Page JSP Page
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

page scope request scope

PageContext request
JSP Page JSP Page JSP JSP Page
Container

JSP
Page
JSP Page
u se
to
B E ense
R NA le lic
B E rab
I L AR nsfeServletContext
session scope HttpSession
GU oscope
application
A n -tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
Scope Rules for the B ER ailฺc Action t u de
R O otm is S
jsp:useBean
h
The figure in the slide illustrates
@ th various scope values for jsp:useBean.
the
• The scope a e
battribute determines the namespace, object reference life cycle, and
e r n
accessibility of an object.
a r b
u•il Objects are always created in response to a request within an instance of a JSP page.
g
(ra • After creating an object, you can request one of four distinct scopes, depending on the
nature of the object and the desired visibility or life cycle.
The page scope
• This attribute defines an object that is accessible only from within the page where it is
created.
• All object references to objects with the page scope attribute are stored in the
PageContext object for the current page.
• They are released when the JSP page is done with processing or the request is
forwarded.

Developing Applications for the Java EE 6 Platform 5 - 34


BS Grupo SฺAฺCฺ
The request scope
• This defines an object that is accessible from all pages that are processed during the
specific client request in which they were created.
• This object reference is stored with the request object.
• They are released when the request is through processing, regardless of how many
times the request is forwarded or how many JSP pages are created.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

The session scope


• You can create these objects only on pages that are session-aware. Session-aware
means that a session object is associated with this page.
• This object is accessible from all pages that are associated with the same session
object. In practice, this means that objects with the session scope are accessible from
all requests from the same client in the same session.
• Object references with session scope are stored with the session object
(HttpSession) and are released when the associated session ends or is invalidated.
The application scope
• These object references are stored with the application object that is associated with a
u se
page activation. to
• B E ense
They are accessible from all pages processing requests that are in the same application
as the one in which they were created.
R NA le lic
• B E rab
Object references with application scope are released when the container reclaims
the ServletContext object.
I L AR nsfe
A GU on-tra
S US s a n deฺ
J E
) h a Gui
R TO com ent
O BE mailฺ Stud
R hot this
a b e@
er n
a r b
g u il
(r a

Developing Applications for the Java EE 6 Platform 5 - 35


BS Grupo SฺAฺCฺ

Request-Scope Beans and Collecting Data


from Servlets
The jsp:useBean action is commonly used to share data
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

between servlets and JSP pages. The following is a typical


sequence of events involving jsp:useBean:
• A servlet performs front-end processing.
• The servlet sets an attribute on the request object.
• The servlet dispatches control to a JSP page to display
dynamic data.
• The JSP uses jsp:useBean with the request scope
attribute to collect the data. u se
to
B E ense
R NA le lic
B E rab
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
B ER ailฺc tude
RO hotm this S
a b e@
er n
a r b
g u il
(r a

Developing Applications for the Java EE 6 Platform 5 - 36


BS Grupo SฺAฺCฺ

Request-Scope Beans and Collecting Data


from Servlets
The following code snippet creates a new Customer object and
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

saves it in a request attribute named customer:

1 public void doPost(HttpServletRequest request, HttpServletResponse


response) {
2 ...
3 try {
4 Customer cust = new Customer(firstName, lastName);
5 request.setAttribute("customer", cust);
6
7 // use a request dispatcher to forward to a JSP page
8 RequestDispatcher disp = u se
to
9 getServletContext().getRequestDispatcher("/jsp/example.jsp");
B E ense
NA le lic
10 disp.forward(request, response);
11 } catch (Exception ex) { R
E rab
12 . . . B
13 } // end catch
I L AR nsfe
14 }
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
B ER ailฺc tude
RO hotm this S
a b e@
n
la rber
u i
(r ag

Developing Applications for the Java EE 6 Platform 5 - 37


BS Grupo SฺAฺCฺ

Request-Scope Beans and Collecting Data


from Servlets
The JSP page example.jsp that is illustrated in the following
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

code snippet can then access and process the customer


object as follows:

1 <jsp:useBean id="customer" class="bank.Customer"


scope="request"/>
2 ...
3 <jsp:getProperty name="customer" property="firstName" />
4
se
<jsp:getProperty name="customer" property="lastName" />
5 ...
to u
B E ense
R NA le lic
B E rab
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
B ER ailฺc tude
RO hotm this S
a b e@
er n
a r b
g u il
(r a

Developing Applications for the Java EE 6 Platform 5 - 38


BS Grupo SฺAฺCฺ

The jsp:getProperty Action


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

JSP Page
getProperty
<jsp:getProperty name=“account” property=“cardNo”/>

account

fName :James
lName :Smith
address :1234 Way St.
city :Seattle

se
state :WA
Phone :(555)555-1212
to u
E ense
cardNo :1234-28
1234-28
B
R NA le lic
B E rab
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
B ER ailฺc tude
Action
jsp:getProperty
R O otm is S
h
The figure in the slide illustrates
@ th use of the jsp:getProperty action.
the
The jsp:getProperty
n a be action gets the value of a property for the named bean instance,
converts b e
it r
to a String, and places it in the implicit out object. The syntax for the
il a r action is:
g u
getProperty
(ra <jsp:getProperty name="bName" property="pName" />
In this syntax example, the bName variable is the name of a previously created bean instance
and pName variable is the name of the property to get. Object instances that are retrieved with
the use of this action are converted to a String value using the object’s toString method.

Developing Applications for the Java EE 6 Platform 5 - 39


BS Grupo SฺAฺCฺ

Custom Tag Libraries

• Provide an alternative to scriptlets for customizing JSP


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

pages for specific business needs


• Use an XML-style tag format to provide canned capabilities
to use, modify, or create objects
It is preferable to avoid scriptlet code in JSP pages.

u se
to
B E ense
R NA le lic
B E rab
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
R ilฺc ude
B E a t
Custom Tag Libraries
RO hotm this S
The JavaServer Pages @ Standard Tag Library (JSTL) is a standard library of tags that is
b e
available fromahttp://java.sun.com/products/jsp/jstl/index.jsp. JSTL encapsulates, as simple
tags, core e r n
functionality that is common to many JSP applications. The use of JSTL is not
r b
la in this course.
covered
g u i
(ra

Developing Applications for the Java EE 6 Platform 5 - 40


BS Grupo SฺAฺCฺ

The taglib Directive

• Extends the set of tags that a JSP container can interpret


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

• Associates a tag prefix with a tag library that:


– Consists of a set of classes and a tag library descriptor
– Implements the range of operations defined in the tags

u se
to
B E ense
R NA le lic
B E rab
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
B ER ailฺc tude
RO hotm this S
a b e@
er n
a r b
g u il
(r a

Developing Applications for the Java EE 6 Platform 5 - 41


BS Grupo SฺAฺCฺ

The taglib Directive

Format of the taglib directive:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

<%@ taglib uri="iterator_tags" prefix="iterator" %>

Tag library usage:

<iterator:iterate>
<%-- perform repetitive task --%>
... u se
to
</iterator:iterate>
B E ense
R NA le lic
B E rab
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
The taglib Directive B ER ailฺc tude
RO hotm this S
Tag library usage:@ The code examples and associated text show the use of a tag library that
b e
implements a general-purpose
athat iterator. JSP page developers use the iterator tag to iterate
over anything
e r n implements the java.util.Iterator interface.
r b
lataglib directive tells the JSP translator that any tag that begins with iterator: has its
g
The
u i
(ra range of operations defined in a custom tag library, which can be located using the identifier
iterator tags.

Developing Applications for the Java EE 6 Platform 5 - 42


BS Grupo SฺAฺCฺ

The tag-library Descriptor and Java Classes

• The URI mapping in the web application deployment


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

descriptor maps a prefix to a TLD.


• The TLD is an XML file that is usually packaged in the web
application or library, along with the classes that implement
its range of operations.

u se
to
B E ense
R NA le lic
B E rab
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

R T
BE mailand ฺc tude
The tag-libraryODescriptor
R hot this Java S Classes
Note: For the Glassfish
@ Application Server the JSTL TLD files can be found inside a JAR file
e
n b
that is locatedaunderneath the App Server installation directory. These TLD files are detected
r
and usedeautomatically. The TLD files are located in the META-INF directory of
i l a rb
glassfish/modules/jstl-impl.jar.
a g u
(r

Developing Applications for the Java EE 6 Platform 5 - 43


BS Grupo SฺAฺCฺ

The tag-library Descriptor and Java Classes

Mapping example:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

• In the JSP page:


<iterator:iterate id="accounts">
• Information from the TLD:
1 <tag>
2 <name>iterate</name>
3 <tagclass>
4 com.acme.tags.IteratorTag
5 </tagclass> u se
to
6 <attribute>
B E ense
7 <name>id</name>
R NA le lic
8 <required>true</required>
B E rab
9 </attribute>
I L AR nsfe
10 </tag>
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

R T
BE mailand ฺc tude
The tag-libraryODescriptor Java
S Classes (continued)
R o t i s
When the JSP translator
@ h encountersth the line <iterator:iterate id="accounts"> in
e which class provides the range of operations to support the
the JSP page,aitbknows
er n
iterator:iterate tag. This information comes from the TLD, which has a section like the
one r b
lashown in slide.
g u i
(ra This <tag> element says that the tag iterate is supported by a class called
IteratorTag. It also says that the iterate tag recognizes one attribute, id, which the JSP
page must supply. In the code in the slide , id is the name of an instance that is already
created within the JSP page, and perhaps passed in from a servlet in the request.
The TLD information allows the JSP translator to generate code that makes method calls on
the class IteratorTag. These method calls are made repeatedly, until the IteratorTag
signals that it has finished. This allows the implementation of a looping tag, which is exactly
the range of operations that this example needs.

Developing Applications for the Java EE 6 Platform 5 - 44


BS Grupo SฺAฺCฺ

The Expression Language (EL)

The Expression Language is an easy-to-use language that can


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

be embedded in JSP pages instead of scriptlets (when used


with tag libraries). Its syntax is similar to JavaScript and can be
learned by nonprogrammers.

${ 3 + 2 }
${ param.address }
${ requestScope.customer.name }

se
${ not empty sessionScope.message }
to u
B E ense
R NA le lic
B E rab
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
R ilฺc ude
B E a t
RO hotm(EL)this S
The Expression Language
The syntax of EL is @ similar to ECMAScript and, therefore, should be easy to leverage for web
e
ab a strong Java background.
developers without
Note: For r n
ea more complete explanation of the Expression Language, refer to the JSP
a r b
g u il
specification or to the course titled Web Component Development with Servlet and JSP
(ra Technologies.

Developing Applications for the Java EE 6 Platform 5 - 45


BS Grupo SฺAฺCฺ

The JSTL Core Tag Library

• Java EE 6 provides several prewritten custom tags known


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

as the JavaServer Pages Standard Tag Library or JSTL.


• These libraries are grouped by functionality.
• The most commonly used library is the core library.

Functional Area URI Prefix

Core http://java.sun.com/jsp/jstl/core c

XML processing http://java.sun.com/jsp/jstl/xml x

118N capable http://java.sun.com/jsp/jstl/fmt fmt u se


to
formatting
B E ense
Relational db access
(SQL)
http://java.sun.com/jsp/jstl/sql sql
R NA le lic
B E rab
Functions http://java.sun.com/jsp/jstl/functions
I L AR nsfe fn
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
B ER ailฺc tude
RO hotm this S
a b e@
b e rn
r
guila
(ra

Developing Applications for the Java EE 6 Platform 5 - 46


BS Grupo SฺAฺCฺ

JSTL Examples

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

<c:forEach var="item" items="${requestScope.list}">


<tr><td>${item.var1}</td><td>${item.var2}</td></tr>
</c:forEach>

<c:if test="${x < 3}" >

</c:if>

<c:choose>

se
<c:when test="${requestScope.message == null}">

to u
</c:when>
B E ense
<c:otherwise>
R NA le lic
B E rab
AR nsfe
</c:otherwise>
</c:choose>
I L
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
B ER ailฺc tude
RO hotm this S
a b e@
b e rn
r
guila
(ra

Developing Applications for the Java EE 6 Platform 5 - 47


BS Grupo SฺAฺCฺ

Packaging Tag Libraries in Web Applications

• With a custom tag library, the tag class must be in the


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

classpath of the class loader. Tag classes can be located


in the same directories as regular classes and jar libraries.
• A Java EE 6 application server implementation can
support additional directories when determining the
classpath.
• The standard tag libraries are already present on any Java
EE 5 system as JAR files. JSTL JAR files have TLD files
embedded in such a way that they do not require them to
u se
be listed in the web.xml file. e to
A BE cens
E RN ble li
R B era
I L A nsf
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
B ER ailฺc tude
RO hotm this S
a b e@
er n
a r b
g u il
(r a

Developing Applications for the Java EE 6 Platform 5 - 48


BS Grupo SฺAฺCฺ

Summary

In this lesson, you should have learned:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

• JSP technology as a presentation mechanism


• The creation of JSP pages
• Data processing in JSP pages
• Custom tag libraries
The following are some interesting topics covered in the course
titled Web Component Development with Servlet and JSP
Technologies.
u se
• Comprehensive JSTL and EL coverage to
• Custom tags B E ense
R NA le lic
• Error handling BE rab
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
R ilฺc ude
B E a t
Summary
RO hotm this S
JSP pages offer a tag-based
@ way to present dynamic data. JSP technology offers all of the
b e
advantages ofaservlets, with increased readability and separation of presentation and
applicatione r n
logic.
a r b
g
JSP
u il pages can be precompiled, or translated and loaded on request if the page has been
( ra modified. Servlets and JSP pages can work together by use of the jsp:useBean action. JSP
pages can also be extended by using custom tag libraries.

Developing Applications for the Java EE 6 Platform 5 - 49


BS Grupo SฺAฺCฺ

Quiz

In an MVC architecture, JSPs are used primarily in which area?


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

a. Model
b. View
c. Controller
d. Model, View, and Controller

u se
to
B E ense
R NA le lic
B E rab
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
B ER ailฺc tude
Answer: b
RO hotm this S
a b e@
er n
a r b
g u il
(r a

Developing Applications for the Java EE 6 Platform 5 - 50


BS Grupo SฺAฺCฺ

Quiz

What does JSTL stand for?


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

a. Java String Tag Library


b. Java Sting and Text Library
c. Java Standard Tag Library
d. Java Standard Text Library

u se
to
B E ense
R NA le lic
B E rab
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
B ER ailฺc tude
Answer: c
RO hotm this S
a b e@
er n
a r b
g u il
(r a

Developing Applications for the Java EE 6 Platform 5 - 51


BS Grupo SฺAฺCฺ

Quiz

When an application server first encounters a JSP page, it


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

does which of the following?


a. Does nothing
b. Compiles the page into a servlet
c. Runs the page through an interpreter
d. Converts the page to HTML and displays the output

u se
to
B E ense
R NA le lic
B E rab
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
B ER ailฺc tude
Answer: b
RO hotm this S
a b e@
er n
a r b
g u il
(r a

Developing Applications for the Java EE 6 Platform 5 - 52


BS Grupo SฺAฺCฺ

Quiz

Which bracket syntax is used to define an expression?


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

a. <%! ... %>


b. <%= ... %>
c. <% ... %>
d. <%@ ... %>

u se
to
B E ense
R NA le lic
B E rab
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
B ER ailฺc tude
Answer: b
RO hotm this S
a b e@
er n
a r b
g u il
(r a

Developing Applications for the Java EE 6 Platform 5 - 53


BS Grupo SฺAฺCฺ

Quiz

Which of the following are Page directive options?


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

a. import
b. include
c. verify
d. errorPage

u se
to
B E ense
R NA le lic
B E rab
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
B ER ailฺc tude
Answer: b
RO hotm this S
a b e@
er n
a r b
g u il
(r a

Developing Applications for the Java EE 6 Platform 5 - 54


BS Grupo SฺAฺCฺ

Quiz

Which of the following examples show the proper use of the


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

include directive?
a. <%@ page file="page.html" %>
b. <%@ include src="page.html" %>
c. <%@ include file="page.html" %>
d. <%@ page include="page.html" %>

u se
to
B E ense
R NA le lic
B E rab
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
B ER ailฺc tude
Answer: c
RO hotm this S
a b e@
er n
a r b
g u il
(r a

Developing Applications for the Java EE 6 Platform 5 - 55


BS Grupo SฺAฺCฺ

Practice 5: Overview

This practice covers the following topics:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ

• Creating a JSP component


• Using JSP scriptlet tags
• Using JSTL tags
• Using the Expression Language (EL)

u se
to
B E ense
R NA le lic
B E rab
I L AR nsfe
A GU on-tra
S an
Copyright © 2011,E S U itssaffiliates. All
Oracle and/ora i d eฺ
O J om) h nt Gu
rights reserved.

T
B ER ailฺc tude
RO hotm this S
a b e@
er n
a r b
g u il
(r a

Developing Applications for the Java EE 6 Platform 5 - 56

You might also like