Professional Documents
Culture Documents
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
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
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
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.
JSP technology:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
T
B ER ailฺc tude
RO hotm this S
a b e@
er n
a r b
g u il
(r a
T
B ER ailฺc tude
RO hotm this S
a b e@
er n
a r b
g u il
(r a
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
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
JSP pages:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
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.
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.
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
( ragdefault.
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
HTML/XML
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
Markup
Directives
Implicit Objects
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
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.
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
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.
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
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
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.
• 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
<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>
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
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
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
<%! <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
<%! <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
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
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
Thread-Safety Implications
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
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.
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
Syntax of jsp:useBean:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
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.
useBean
1
scope
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
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.
Examples of jsp:useBean:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
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
JSP Page JSP Pages JSP Page JSP Page JSP Page
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
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.
T
B ER ailฺc tude
RO hotm this S
a b e@
er n
a r b
g u il
(r a
T
B ER ailฺc tude
RO hotm this S
a b e@
n
la rber
u i
(r ag
T
B ER ailฺc tude
RO hotm this S
a b e@
er n
a r b
g u il
(r a
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.
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
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
<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.
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
Mapping example:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
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.
${ 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.
Core http://java.sun.com/jsp/jstl/core c
T
B ER ailฺc tude
RO hotm this S
a b e@
b e rn
r
guila
(ra
JSTL Examples
</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
T
B ER ailฺc tude
RO hotm this S
a b e@
er n
a r b
g u il
(r a
Summary
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.
Quiz
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
Quiz
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
Quiz
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
Quiz
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
Quiz
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
Quiz
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
Practice 5: Overview
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