bYTEBoss jstl

Download Report

Transcript bYTEBoss jstl

JSTL and Web Development at
Universities
Shawn Bayern
Research programmer, Yale University
 JSTL reference-implementation lead
 Author, Web Development with JavaServer Pages
 Author, The JSP Standard Tag Library (upcoming)

Agenda


JSP overview: scripting and tag libraries
JSTL basics




Examples of design principles in use



Origins
Design
Features
Yale’s network-registration application
HTML “scraping” for portals
Q&A
JavaServer Pages
Scriptlets

<%
getFoo(request);
printFoo(out);
String a = “goat”;

%>
<%if (a.equals(“pig”) {%>
Oink!
<%}%>

Java (and more?)
embedded within
template text
Access to implicit
objects: request,
response, etc.
Conditional blocks,
loops—manually
constructed
JavaServer Pages
Tag libraries

<foo:bar/>
<c:if test=“c”>
c is true
</c:if>
<c:forEach>
Round and round
we go
</c:forEach>




HTML-like tags (can be
well-formed XML)
Invoke Java logic
behind the scenes.
May access body, e.g.,
for iteration, conditional
exclusion—or just as
arbitrary parameter
May access
PageContext
Libraries and prefixes
Advantages of tag libraries
Abstraction, abstraction, abstraction.
 Abstraction.

Separation of logic from presentation
and content.
 Simple, familiar interface for page
authors.
 Implicit (versus explicit) arguments.

Tags: Abstracting logic
Tag
library
Database
JSP
page
Back-end
Java
code
Tag
library
Directory
What is a JSP tag?


“Standard actions” – e.g.,<jsp:useBean>
 Part of JSP spec
Java class implementing Tag interface
 Accepts attributes as JavaBean properties
(setXXX())
 Answers callbacks associated with
•
•
•
•



Start of tag
End of body
End of tag
… and so on
Why so specific? (No closure for body.)
Knows about its page (PageContext)
Knows about its parent tags
• findAncestorWithClass()

Optionally knows about its body (BodyContent)
Tag handler
Tag
attributes
doStartTag()
doCatch()
Tag
body
doEndTag()

Tag handler
doFinally()
Where do tag libraries come
from?

Java developers like us



To support ourselves, our page authors
Vendors—e.g., Allaire, BEA, iPlanet
Standard tag library




Java Community Process (JSR-052)
Input from familiar vendors, motivated
individuals
Under Sun’s leadership
Implementation distributed through Apache
JSTL design principles
JSTL 1.0: Keep it simple!
 Targeted tags


Could have a single <go> tag:
• <go action=“forEach”
argument1=“blargh”>


Instead, single-purpose tags, tightly
focused
Design motivated by “page author”

Perhaps something of a fantasy, like the legal “reasonable
person.” But a helpful guide nonetheless.
The parable of
Mike and Phillipe
Mike
Credit: Pierre Delisle (spec lead)
Phillipe
Potential limitations


Tag abstraction may be excessive for tiny
applications (but JSP 1.3…)
Page authors need technical skills! They
must understand:




Flow of application
Programmatic control flow within page
Variable reference (expression language)
Domain-specific languages (SQL, XPath)
and associated principles
JSTL 1.0 features
Conditional logic
 Iteration
 Text retrieval (URL, RequestDispatcher)
 Text formatting and parsing (i18n)
 XML manipulation (XPath, XSLT)
 Database access
 Last but certainly not least:


Expression language
JSTL 1.0 libraries
Library features
Recommended prefix
Core (control flow,
URLs, variable access)
Text formatting
c
XML manipulation
x
Database access
sql
fmt
JSTL features:
expression language
JSTL’s major structural innovation
 Replaces “rtexprvalues”



Allows simple retrieval of scoped
attributes…


<%= pageContext.getAttribute(“duck”) %>
$session:duck
… and other data


$cookie:crumb
$param:email
JSP history: progress
Scriptlets
<%= findName(request.getParameter(“netid”)) %>
Beans and standard actions
<jsp:useBean id=“netid” class=“edu.yale.its.Netid”/>
<jsp:setProperty name=“netid” property=“netid”/>
<jsp:getProperty name=“netid” property=“name”/>
Traditional tag libraries
rtexprvalu
e
<yale:fullName netid=‘<%= request.getParameter(“netid”)) %>’/>
Expression-language capable library
<yale:fullName netid=“$netid”/>
State of expression
language
Syntax currently being debated
 EA2 supports “pluggable” languages
for experimentation. Candidates
include:

SPEL (straw man)
 ECMAScript
 JXPath


Comments?
Advantages of expression
language

Scripting, not programming. Not even
scripting, really.



Having said that, I personally hope that the EL(s) we
standardize on are so simple as to require almost no
education at all.
+2
Typeless reference
 Simplified syntax

JSTL features:
core tags (1)

Iteration
<c:forEach
items=“$list”
begin=“5” end=“20” step=“4”
var=“item”>
<c:expr value=“$item”/>
</c:forEach>
“var” convention and variable access
 “paging”

JSTL features:
core tags (2)

Conditional evaluation
<c:if test=“$a = b”>
a equals b
</c:if>

Mutually exclusive
conditionals
<c:choose>
<c:when test=“$a = b”>
a equals b
</c:when>
<c:when test=“$a = c”>
a equals c
</c:when>
<c:otherwise>
I don’t know what
‘a’equals.
</c:otherwise>
</c:choose>
JSTL features:
core tags (3)

URL retrieval
<c:import var=“cnn”
url=“http://www.cnn.com/cnn.rss”/>



Data exposed as String or Reader
All core URLs supported (HTTP, FTP, HTTPS
with JSSE)
Local, cross-context imports supported
JSTL features:
text formatting

Formatting and parsing



Internationalization



Numbers
Dates
Message bundles
Locale support
Message argument substitution

“Hi {0}. I would like to {1} your money today.
I will use it to buy myself a big {2}.”
JSTL features:
XML manipulation

Use of XSLT, XPath to access, display
pieces of XML documents
<c:import url=“http://www.cnn.com/cnn.rss” var=“cnn”/>
<x:parse source=“$cnn” var=“dom”>
<x:expr value=“$dom//item[1]/title”/>

Example later
JSTL features:
database manipulation

Tags for

Queries
• ResultSet caching
Updates / inserts
 Transactions
 Parametric (PreparedStatement)
argument substitution
 DataSource-based connection
management

SQL tags: the debate
Tag
library
JSP
page
Back-end
Java
code
Database
Tag
library
JSTL programmer support

JSTL also supports Java developers
Simplifies tag development
 IteratorTagSupport,
ConditionalTagSupport
 Instead of writing whole tag handler
(doStartTag(), doEndTag()), simply
override a few methods:

• protected boolean condition()
• protected Object next()
JSTL schedule

Currently in Early Access Release 2

No promises, but many hints.

http://jakarta.apache.org/taglibs/ → “Standard”
EA3 mid December
 Release of 1.0 in 2002

JSTL 1.0 to include core features
 Later versions fill in other convenient
but less general functionality.

• Perhaps more J2EE-specific support.
JSP/JSTL future
Finalize JSTL
 JSP 1.3

Universalize “expression language”
 Support creation of tags in JSP itself

• Works nicely with JSTL’s focused tags

Books, educational materials on JSTL
to assist page authors (and us
developers vicariously).
Yale example: Netreg

Student network registration (Netreg)
application





Used by 5900 students to register computers
for network use
Discovers EA via SNMP router query
Managed by Student Computing group at
Yale; my group provided support
Developed in less than a month
Student Computing group
(nonprogrammers) maintains user interface
Yale example: Netreg


HTML, JSP code
produced and
maintained by
nonprogrammers
Application uses
early-access
“standard” – plus
locally written –
tags.
Yale example: Netreg

Example code

Retrieve full name from database
<reg:fullName
var="name“ netid=“$netid"/>

Conduct CAS authentication
<cas:auth var="netid" scope="session"/>

Discover Ethernet address
<reg:eha var="eha" scope="session"
onError="discoveryFailed.jsp"/>
Yale example: Netreg
Custom “registration” tags
documented, available to page
authors.
 Via attributes, the data these tags
expose is accessible to JSTL tags.

Yale example: XML/XPath
Portals, other “summary” web sites
often need to retrieve content from
other pages.
 JSTL provides a flexible, standard
way to handle this task.
 Example: retrieve headlines from
student newspaper, display in custom
format.

Yale example: XML/XPath
<c:import var="ydn"
url="http://www.yaledailynews.com/"/>
<tidy:parse input=“$ydn” output=“ydn”/>
<x:parse var="dom" source="$ydn"/>
<%-- Print the lead headline --%>
<x:expr
select="$dom//a[@$class='leadheadline’]”/>
Sample XML:
<table>
…
<a class=“leadheadline” href=“article.asp”>Headline</a>
…
</table>
Advantages of JSTL
XML/XPath support

Why not always use XSLT?

JSTL provides XPath support but
doesn’t preclude convenient, standard
access to Java/JSP code.
• E.g., parse an article URL out of a
document, then follow the URL and parse
its contents.

JSP/JSTL may be more familiar and
convenient for simple tasks.
Summary
JSP supports scriptlets (poor
abstraction) and tags (useful
abstraction)
 JSTL is the JSP Standard Tag Library.
 JSTL and JSP tags support
separation of content from
presentation

URLs
Jakarta Taglibs
http://jakarta.apache.org/taglibs/
JSTL JSR
http://www.jcp.org/jsr/detail/052.jsr
JSP 1.3 JSR
http://www.jcp.org/jsr/detail/152.jsr
Official email address for JSTL comments
mailto:[email protected]
My upcoming JSTL book’s site
http://www.jstlbook.com/
My email address
mailto:[email protected]