JSPXML
JSPXML
and XML
XML (eXtensible Markup Language) is a set of syntax rules and guidelines for
defining text-based markup languages. XML languages can be used for:
• Exchanging structured information
• Defining structured document types
• Specifying structured messages
Information that is expressed in a structured, text-based format can easily be
transmitted between, transformed, and interpreted by entities that understand the
structure. In this way XML brings the same cross-platform benefits to informa-
tion exchange as the JavaTM programming language has for processing.
JavaServer PagesTM (JSPTM) technology provides a number of capabilities that
are ideally suited for working with XML. JSP pages can contain any type of text-
based data, so it is straightforward to generate documents that contain XML
markup. Also, JSP pages can easily access programming language objects to
parse and transform XML messages and documents.
This paper highlights the ways that JSP pages can consume XML data and gen-
erate XML-based markup for various types of Web-based clients.
1
2 JAVASERVER PAGES AND XML
retrieves the data using JSP expressions. An XML data source is accessed in
essentially the same way as any other data source.
XML
Bean
JSP Page
JDBC
Custom
Tag
Enterprise
Bean
There are two ways you could use this data in a JSP page:
• Convert the XML elements into server-side objects and then use JSP
expressions to access the objects.
• Invoke an XSLT transformation on the XML data, preferably through a
custom tag.
<html>
<tl:parse id=”inventory” xml=”XML_Book_Inventory”/>
<head>
<title>Book Inventory</title>
</head>
<body>
<table border="1" width="100%">
<tr>
<th>ISBN</th>
<th>Title</th>
<th>Quantity</th>
<th>Price</th>
</tr>
<tl:iterate id ="book" type=”Book”
collection=”<%= inventory.getBooks() %>” >
<tr>
<td>
<jsp:getProperty name="book" property="id"/>
4 JAVASERVER PAGES AND XML
</td>
<td>
<jsp:getProperty name="book"
property="title"/>,
</td>
<td>
<jsp:getProperty name="book"
property="quantity"/>
</td>
<td>
<jsp:getProperty name="book"
property="price"/>
</td>
</tr>
</tl:iterate>
</table>
</body>
</html>
One way to apply the stylesheet is to use the scripting capabilities of JSP tech-
nology to programmatically invoke the XSLT transformation by calling the
methods of an XSLT processor.
A better way is to use a custom tag to invoke the transformation. The jakarta-
taglibs project at the Apache Software Foundation hosts a tag library (xsl) that
contains a number of tags for processing XML input and applying XSL transfor-
6 JAVASERVER PAGES AND XML
mations. The following JSP page uses the xsl:apply custom tag to apply the
XSL stylesheet listed to an XML document containing the book inventory:
<%@taglib uri="http://jakarta.apache.org/taglibs/xsl-1.0"
prefix="xsl" %>
<html>
<head>
<title>Book Inventory</title>
</head>
<body>
<xsl:apply xml="XML_Book_Inventory"
xsl="inventoryTable.xsl"/>
</body>
</html>
The result of executing this JSP page on the Tomcat JSP and servlet implementa-
tion appears below.
servers that consume XML can also be clients. The scenario we would like to
support looks like the following:
Browser
PDA
XSLT Beans
Phone
(wireless,
JSP
landline)) Custom
Pages
Tags
Server
Supporting all of these clients will dwarf the effort expended on HTML-based
applications during the 1990s. Fortunately better tools are available now: JSP
technology and XML. Next we describe how to use these tools to develop multi-
lingual Web applications.
8 JAVASERVER PAGES AND XML
Single Pipeline
In the single pipeline approach, a JSP page applies an XSLT transformation to
incoming XML data to generate client-specific markup. Each type of client
GENERATING XML USING JSP PAGES 9
requires a different stylesheet and the bulk of the development costs are associ-
ated with creating and maintaining these stylesheets.
HTML
XSLT
WML Parse and Transform JSP Page XML
via Custom Tag
XSL(HTML)
XML
XSL(WML)
XSL(XML)
This approach defers generation of both the static and dynamic portions of a
response to runtime. The runtime costs are associated with:
• Parsing the XML data
• Parsing the XSLT stylesheet
• Applying the XSLT transformation
To improve the performance of transformations, you can create a binary repre-
sentation of a stylesheet using an XSLT compiler. However, this also makes the
maintenance process more complex: Each time the presentation is changed, the
stylesheet must be recompiled.
Note that generating the presentation for clients with different interaction models
and flow of control (for example, PC-based browsers versus WAP phones) will
probably require very different transformations. For example, a mobile phone
cannot display a table containing book inventory data. Instead the data would
have to be displayed as a set of nested lists. Supporting such transformations
increases both the development and runtime costs.
10 JAVASERVER PAGES AND XML
Multiple Pipeline
The multiple pipeline approach uses a set of client-specific JSP pages to generate
output.
Bean
WML JSP Page XML
Custom
Tag
XML
JSP Page
As compared with using XSLT transformations, this approach keeps the work of
creating the static portions of the presentation in the development phase, with the
dynamic portion occurring at runtime.
Aside from creating the client-specific JSP pages, development costs are
incurred in creating and maintaining server-side objects that represent the appli-
cation’s data abstractions. This step is not required in the single pipeline
approach. Nevertheless the multiple pipeline approach can be more cost effective
than the single pipeline for the following reasons:
• Data abstractions can be reused by different kinds of JSP pages.
• Data abstractions typically change at a much lower rate than presentation.
• Executing a JSP page to generate a user interface markup is much more
efficient than performing an XSLT transformation to generate the same
markup.
The following table summarizes the costs of each approach:
Combination
It is also possible to combine the two approaches. If your clients speak different
languages you should probably use multiple pipelines. However, to generate dia-
lects of a single language, you can apply an XSLT transformation to that lan-
guage’s pipeline.
a
WML Bean
XML
XSLT JSP Page
b
WMLL Custom
Tag
XML
JSP Page
Conclusion
JavaServer Pages technology and XML are natural partners for developing Web
applications that use heterogeneous data sources and support multi-lingual cli-
ents. This paper has described several approaches to addressing these require-
ments that trade off development and maintenance versus runtime costs.
12 JAVASERVER PAGES AND XML