0% found this document useful (0 votes)
44 views28 pages

SCWCD - JSP Presentation

The document discusses new features in JSP 2.0 including an integrated expression language that allows accessing objects and properties with EL expressions like ${item.price} instead of scriptlets, simple tag extensions that simplify creating custom tags for both page authors and tag library developers, and tag files that allow defining tag attributes outside of JSP pages.

Uploaded by

api-3701529
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views28 pages

SCWCD - JSP Presentation

The document discusses new features in JSP 2.0 including an integrated expression language that allows accessing objects and properties with EL expressions like ${item.price} instead of scriptlets, simple tag extensions that simplify creating custom tags for both page authors and tag library developers, and tag files that allow defining tag attributes outside of JSP pages.

Uploaded by

api-3701529
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

10/12/2003

JSP 2.0 (in J2EE 1.4)

1
10/12/2003

Sang Shin
[email protected]

www.javapassion.com/j2eeadvanced/

2
10/12/2003

Disclaimer & Acknowledgments


? Even though Sang Shin is a full-time employees of Sun
Microsystems, the contents here are created as his own personal
endeavor and thus does not reflect any official stance of Sun
Microsystems.
? Sun Microsystems is not responsible for any inaccuracies in the
contents.
? Acknowledgements
– Many slides are borrowed from “Servlet 2.4 and JSP 2.0
specification” JavaOne 2003 presentation by Mark Roth of Sun
Microsystems
– The slides, speaker notes, and example code of this
presentation are created from
? “ Custom Tags” section of Java WSDP 1.2 tutorial written by

Stephanie Bodoff of Sun Microsystems

3
10/12/2003

Revision History
? 10/13/2003: version 1: created by Sang Shin

4
10/12/2003

Agenda
? Focus of JSP 2.0 technology
? Examples of syntax
? Exploration of new features (with
examples)
– Integrated expression language
– Simple tag extensions
– Tag files
– Improved XML syntax
– Other features

5
10/12/2003

Focus of JSP 2.0 Technology


? Ease of use – lowers the bar

User Class HTML XML Java

Tag Library Developer


JSP 2.0
JSP 1.2

Advanced Page Author

Basic Page Author

M
I
D
D = Basic Knowledge = Expert
L
E
6

6
10/12/2003

JSP 1.2 Syntax With Scriptlets


<%-- Output Shopping Cart --%>
<%@ page import="com.acme.util.*" %>
<%@ taglib prefix="util" uri="http://mytaglib" %>

<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>
M
I
<% } %>
D
D
</table>
L
E
</body>
7
</html>

7
10/12/2003

JSP 2.0 Syntax Without Scriptlets


<%-- Output Shopping Cart --%>
<%@ taglib prefix="util" uri="http://mytaglib" %>
<%@ taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
<util:getShoppingCart var="cart" />
<table>
<c:forEach var="item" values="${cart}">
<tr>
<td>${item.name}</td>
<td>${item.price}</td>
</tr>
</c:forEach>
M </table>
I
D </body>
D
L </html>
E
8

8
10/12/2003

JSP 2.0 Improved XML Syntax


<!-- Output Shopping Cart -->
<html xmlns:util="http://mytaglib"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<body>
<util:getShoppingCart var="cart" />
<table>
<c:forEach var="item" values="${cart}">
<tr>
<td>${item.name}</td>
<td>${item.price}</td>
</tr>
</c:forEach>
</table>
</body>
M </html>
I
D
D
L
E
9

9
10/12/2003

Integrated
Expression Language

10

10
10/12/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)}
M
I
D
– JSTL 1.1 provides 16 standard EL functions
D
L
E
11

11
10/12/2003

Integrated Expression
Language Example
? Using scriptlets:

<center>
<jsp:useBean id="foo" class="FooBean" />
<%= foo.getBar() %>
</center>

? Equivalent, using an EL expression:

M
<center>
I
D
${foo.bar}
D
L
</center>
E
12

12
10/12/2003

Integrated Expression
Language Example
? Using scriptlets:

<% Map m = (Map)pageContext.getAttribute(


"state" );
State s = ((State)m.get( "NY" ));
if( s != null ) {
%>
<%= s.getCapitol() %>
<% } %>

? Equivalent, using an EL expression:


M
I
D
D
L
${state["NY"].capitol}
E
13

13
10/12/2003

Simple Tag Extensions

14

14
10/12/2003

Comparison of Tag Extensions


? Classic tag extensions (JSP 1.2
technology)
– Inverted tag handler API
– Written only in the Java programming language
– Created only by tag library developers
? Simple tag extensions (JSP 2.0 technology)
– Simpler tag handler API
– Written in the Java programming language or using
JSP syntax

M
I
D
Created by page authors or tag library developers
D
L
E
15

15
10/12/2003

Repeat Tag Implemented as a


Classic JSP 1.2 Tag Extension
<%@ taglib prefix="my"
uri="/mytags" %> Tag
<my:repeat num ="3">
Usage

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()
M
I
return (this.count > 0) ? doAfterBody()
D Tag.EVAL_BODY_AGAIN : d
D doEndTag()
L
E
Tag.SKIP_BODY;
16
}

16
10/12/2003

Repeat Tag Implemented as a


Simple JSP 2.0 Tag Extension
<%@ taglib prefix="my" SimpleTag
Usage

uri="/mytags" %>
<my:repeat num ="3"> SimpleTagSupport
tag body
</my:repeat>
RepeatHandler
setNum()
doTag()
void doTag() {
Implementation

for( int i = 0; i < num; i++ ) {


getJspBody().invoke( null );
}
}
M
I
D
D
L
E
17

17
10/12/2003

Tag Files

18

18
10/12/2003

Tag Files
? Quickly write tag extensions using JSP
syntax!
? JSP : Servlet :: Tag File : Tag Handler
? Empowers page authors
? Faster round-trip development
? Simple yet flexible packaging
– Just drop a .tag file in /WEB-INF/tags/
– Implicit tag library automatically generated
M
– Or, write a .tld for added flexibility
I
D
D
– Or, package in a JAR with a .tld
L
E
19

19
10/12/2003

Repeat Tag Implemented as a


Tag File
<%@ taglib prefix="my"
Usage

tagdir="/WEB-INF/tags/" %>
<my:repeat num ="3">
tag body
</my:repeat>

<%-- /WEB-INF/tags/repeat.tag --%>


Implementation

<%@ attribute name="num" %>


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

<c:forEach begin="1" end="${num}">


M
I <jsp:doBody />
D
D </c:forEach>
L
E
20

20
10/12/2003

Declaring a tag library: tagdir


attribute (JSP 2.0 only)
? Identifies the location of the tag files
– tag file concept is introduced in JSP 2.0
– tag file is tag library implementation in JSP syntax
rather than Java code (we will talk about this later on)
? Value of it must start with /WEB-INF/tags/

21

Tag file concept is introduced in JSP 2.0. Tag file is tag handler
implementation in JSP syntax as opposed to using Java programming
language.

The value of tagdir attribute starts with /WEB-INF/tags.

21
10/12/2003

Improved
XML Syntax

22

22
10/12/2003

Improved XML Syntax:


JSPX and TAGX
? Finally, no more need for <jsp:root>!
? JSP technology as an XML namespace
? Ideal for outputting XML content like SVG:

<!-- images/hello.svg -->


<svg xmlns:c="http://java.sun.com/jsp/jstl/core"
width="8.5in" height="11.0in">
<c:forEach var="y" begin="3" end="10">
<text style="fill:blue;" y="${y*15}">
"Hello, ${firstName} ${lastName}!"
</text>
M </c:forEach>
I
D </svg>
D
L
E ? JSP documents (.jspx) or Tag files (.tagx) 23

23
10/12/2003

Other Features

24

24
10/12/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
M
I
? Fragment attributes
D
D
L
E
25

25
10/12/2003

Resources

26

26
10/12/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)

27

This is the resource page.

27
10/12/2003

Live your life


with Passion!

28

28

You might also like