jstl and web development at universities shawn bayern research programmer, yale university jstl...
Post on 19-Dec-2015
214 views
TRANSCRIPT
![Page 1: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/1.jpg)
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)
![Page 2: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/2.jpg)
Agenda
JSP overview: scripting and tag libraries JSTL basics
Origins Design Features
Examples of design principles in use Yale’s network-registration application HTML “scraping” for portals
Q&A
![Page 3: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/3.jpg)
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
![Page 4: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/4.jpg)
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
![Page 5: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/5.jpg)
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.
![Page 6: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/6.jpg)
Taglibrary
Tags: Abstracting logic
JSPpage
Back-endJavacode
Database
Directory
Taglibrary
![Page 7: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/7.jpg)
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)
![Page 8: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/8.jpg)
Tag handler
Tag handler
doStartTag() doEndTag()
doCatch() doFinally()
Tagattributes
Tagbody
![Page 9: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/9.jpg)
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
![Page 10: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/10.jpg)
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.
![Page 11: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/11.jpg)
The parable of Mike and Phillipe
Mike Phillipe
Credit: Pierre Delisle (spec lead)
![Page 12: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/12.jpg)
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
![Page 13: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/13.jpg)
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
![Page 14: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/14.jpg)
JSTL 1.0 libraries
Library features Recommended prefix
Core (control flow, URLs, variable access)
c
Text formatting fmt
XML manipulation x
Database access sql
![Page 15: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/15.jpg)
JSTL features:expression language
JSTL’s major structural innovation Replaces “rtexprvalues”
<%= pageContext.getAttribute(“duck”) %>
Allows simple retrieval of scoped attributes… $session:duck
… and other data $cookie:crumb $param:email
![Page 16: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/16.jpg)
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<yale:fullName netid=‘<%= request.getParameter(“netid”)) %>’/>
Expression-language capable library<yale:fullName netid=“$netid”/>
rtexprvalue
![Page 17: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/17.jpg)
State of expression language
Syntax currently being debated EA2 supports “pluggable” languages
for experimentation. Candidates include:SPEL (straw man)ECMAScriptJXPath
Comments?
![Page 18: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/18.jpg)
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
![Page 19: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/19.jpg)
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”
![Page 20: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/20.jpg)
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>
![Page 21: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/21.jpg)
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
![Page 22: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/22.jpg)
JSTL features:text formatting Formatting and parsing
Numbers Dates
Internationalization 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}.”
![Page 23: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/23.jpg)
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
![Page 24: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/24.jpg)
JSTL features:database manipulation Tags for
Queries• ResultSet caching
Updates / insertsTransactionsParametric (PreparedStatement)
argument substitutionDataSource-based connection
management
![Page 25: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/25.jpg)
SQL tags: the debate
Taglibrary
JSPpage
Back-endJavacode
DatabaseTag
library
![Page 26: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/26.jpg)
JSTL programmer support
JSTL also supports Java developersSimplifies tag developmentIteratorTagSupport,
ConditionalTagSupportInstead of writing whole tag handler
(doStartTag(), doEndTag()), simply override a few methods:
• protected boolean condition()• protected Object next()
![Page 27: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/27.jpg)
JSTL schedule
Currently in Early Access Release 2No 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 featuresLater versions fill in other convenient
but less general functionality.• Perhaps more J2EE-specific support.
Applies tothis presentation too!
![Page 28: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/28.jpg)
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).
![Page 29: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/29.jpg)
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
![Page 30: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/30.jpg)
Yale example: Netreg
HTML, JSP code produced and maintained by nonprogrammers
Application uses early-access “standard” – plus locally written – tags.
![Page 31: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/31.jpg)
Yale example: Netreg
Example codeRetrieve 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"/>
![Page 32: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/32.jpg)
Yale example: Netreg
Custom “registration” tags documented, available to page authors.
Via attributes, the data these tags expose is accessible to JSTL tags.
![Page 33: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/33.jpg)
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.
![Page 34: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/34.jpg)
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>
![Page 35: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/35.jpg)
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.
![Page 36: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/36.jpg)
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
![Page 37: JSTL and Web Development at Universities Shawn Bayern Research programmer, Yale University JSTL reference-implementation lead Author, Web Development with](https://reader038.vdocument.in/reader038/viewer/2022110322/56649d3f5503460f94a1836a/html5/thumbnails/37.jpg)
URLs
Jakarta Taglibshttp://jakarta.apache.org/taglibs/
JSTL JSRhttp://www.jcp.org/jsr/detail/052.jsr
JSP 1.3 JSRhttp://www.jcp.org/jsr/detail/152.jsr
Official email address for JSTL commentsmailto:[email protected]
My upcoming JSTL book’s sitehttp://www.jstlbook.com/
My email addressmailto:[email protected]