Professional Documents
Culture Documents
JSP 2.0 (In J2EE 1.4)
JSP 2.0 (In J2EE 1.4)
1
10/15/2003
Sang Shin
sang.shin@sun.com
www.javapassion.com/j2eeadvanced/
2
10/15/2003
3
10/15/2003
Revision History
? 10/13/2003: version 1: created by Sang Shin
4
10/15/2003
Agenda
? Focus of JSP 2.0 technology
? New features
– Integrated expression language
– Simple tag extensions
– Tag files
– Improved XML syntax
– Other features
5
10/15/2003
JSP 2.0
<html>
<body>
<util:getShoppingCart var="cart" />
<table>
<% for( int i = 0; i < cart.size(); i++ ) {
CartItem item=(CartItem)cart.get(i);
%>
<tr>
<td><%= item.getName() %></td>
<td><%= item.getPrice() %></td>
</tr>
<% } %>
</table>
</body>
7
</html>
10/15/2003
9
10/15/2003
Integrated
Expression Language
10
10/15/2003
Integrated Expression
Language
? Based on “SPEL” from JSTL 1.0
? Example: ${item.price}
? Recognized by JSP container in:
– Template text
– Attributes of any standard or custom action
? Programmatic access via
javax.servlet.jsp.el
? Support for custom EL functions:
– Extensible via tag libraries
– Example: ${fn:allCaps(lastName)}
– JSTL 1.1 provides 16 standard EL functions 11
10/15/2003
Integrated Expression
Language Example
? Using scriptlets:
<center>
<jsp:useBean id="foo" class="FooBean" />
<%= foo.getBar() %>
</center>
Integrated Expression
Language Example
? Using scriptlets:
<% Map m = (Map)pageContext.getAttribute(
"state" );
State s = ((State)m.get( "NY" ));
if( s != null ) {
%>
<%= s.getCapitol() %>
<% } %>
14
10/15/2003
15
10/15/2003
16
10/15/2003
17
10/15/2003
JspFragment interface
19
10/15/2003
20
10/15/2003
21
10/15/2003
tag body
</my:repeat> BodyTag IterationTag
TagSupport
int doStartTag() {
b BodyTagSupport
this.count = this.num;
Implementation
return Tag.EVAL_BODY_INCLUDE;
} RepeatHandler a
setNum()
int doAfterBody() { c doStartTag()
this.count--; doInitBody()
return (this.count > 0) ? doAfterBody()
Tag.EVAL_BODY_AGAIN : d
doEndTag()
Tag.SKIP_BODY;
22
}
10/15/2003
uri="/mytags" %>
<my:repeat num ="3"> SimpleTagSupport
tag body
</my:repeat>
RepeatHandler
setNum()
doTag()
void doTag() {
Implementation
23
10/15/2003
Tag Files
24
10/15/2003
Tag Files
? A source file that contains a fragment of
JSP code that is reusable as a custom
tag
? Allow you to create custom tags using
JSP syntax
– Empowers page authors
– Faster round-trip development
? Get translated into a tag handler and then
compiled
– JSP : Servlet :: Tag File : Tag Handler
? No TLD file required 25
10/15/2003
Tag Files
? Simple yet flexible packaging
– Just drop a .tag file in /WEB-INF/tags/
– Implicit tag library automatically generated
– Or, write a .tld for added flexibility
– Or, package in a JAR with a .tld
26
10/15/2003
27
10/15/2003
31
10/15/2003
“ variable” Directive
? Declares EL variables
? EL variables
– It emulates OUT (from tag file to calling page) type
– not initialized by the calling page
– set by the tag file
– Tag attributes emulate IN (from calling page to tag
file) type
32
10/15/2003
33
10/15/2003
34
10/15/2003
36
10/15/2003
<c:choose>
<c:when test="${book.onSale}" >
<jsp:invoke fragment="onSale" />
</c:when>
<c:otherwise>
<jsp:invoke fragment="normalPrice" />
</c:otherwise> 37
</c:choose> </td>
10/15/2003
<tr>
<td bgcolor="#ffffff">
<fmt:message key="By"/> <em>${book.firstName}
${book.surname}</em></td></tr>
</c:forEach>
</table>
</center>
38
10/15/2003
<br>
<br>
<h3><fmt:message key="Choose"/></h3>
40
10/15/2003
41
10/15/2003
42
10/15/2003
tagdir="/WEB-INF/tags/" %>
<my:repeat num ="3">
tag body
</my:repeat>
Improved
XML Syntax
44
10/15/2003
Other Features
46
10/15/2003
Other Features...
? Central configuration via url-patterns
– Map extensions other than .jsp
– Enable / disable scriptlets or EL globally
– Control page encoding globally
– Preludes / codas
? Portable debugging support through JSR-45
? Dynamic attributes
? Enhanced I18N support
? Fragment attributes
47
10/15/2003
Resources
48
10/15/2003
Resources
? [1] Java Web Services Developer Pack Tutorial
– java.sun.com/webservices/downloads/webservicespack.html
– java.sun.com/webservices/downloads/webservicestutorial.html
? [2] More Servlets and JavaServer Pages (written
by Marty Hall)
49
10/15/2003
50