0% found this document useful (0 votes)
61 views12 pages

JSPXML

The document discusses how JavaServer Pages (JSP) can be used to consume and generate XML data. It describes two approaches to using XML data in JSP pages: 1) converting XML elements to server-side objects that can then be accessed, and 2) applying an XSLT transformation to the XML data. It also explains how JSP pages can generate XML markup for different types of clients.

Uploaded by

api-3772730
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)
61 views12 pages

JSPXML

The document discusses how JavaServer Pages (JSP) can be used to consume and generate XML data. It describes two approaches to using XML data in JSP pages: 1) converting XML elements to server-side objects that can then be accessed, and 2) applying an XSLT transformation to the XML data. It also explains how JSP pages can generate XML markup for different types of clients.

Uploaded by

api-3772730
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/ 12

JavaServer Pages

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.

Using XML Data Sources in JSP Pages


JSP pages can easily use a variety of data sources. The following figure illus-
trates the standard way to do so. A JSP page delegates connecting to and retriev-
ing data from data sources to server-side objects or custom tags and then

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.

Clients Web Layer Data Sources

XML

Bean
JSP Page
JDBC
Custom
Tag

Enterprise
Bean

Consider a database that maintains book inventory information. An XML repre-


sentation of the book inventory might be:

<?xml version="1.0" encoding="ISO-8859-1"?>


<books>
<book isbn="123">
<title>Web Servers for Fun and Profit</title>
<quantity>10</quantity>
<price>$17.95</price>
</book>
<book isbn="456">
<title>Web Programming for Mobile Devices</title>
<quantity>2</quantity>
<price>$38.95</price>
</book>
<book isbn="789">
<title>Duke: A Biography of the Java Evangelist</title>
<quantity>25</quantity>
<price>$15.00</price>
</book>
</books>
USING XML DATA SOURCES IN JSP PAGES 3

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.

Convert XML to Server-Side Objects


The first approach requires the JSP page to parse the XML data and create
server-side objects for each of the elements in the XML schema. Currently a
developer has to manually define the objects. In the future this process will be
easier, because the XML Data Binding Specification (JSR 31) will define tech-
nology that will automatically generate Java classes from a given XML schema.
The following example retrieves XML data from the URL XML_Book_Inventory
and uses the data to generate an HTML page that displays the inventory. The
example uses a custom tag to parse the XML data and store it an object that con-
tains a collection of book objects. The example then uses a custom tag to iterate
through the collection, extracting properties of the book objects.

<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>

Convert XML Using XSLT Transformation


The second approach applies an XLST transformation to convert the XML data
into another format. XSLT can be used to transform XML data to HTML, PDF,
or another XML format. For example, you can use XSLT to convert an XML
document in a format used by one company to the format used by another com-
pany.
To generate the HTML page discussed in the previous section you need an XSLT
stylesheet and a way to apply the stylesheet.
The following XSL stylesheet performs the required transformation. The
xsl:template statement declares a template that matches a books element. The
template generates boilerplate HTML table markup and extracts data from the
book elements contained in books.

<?xml version="1.0" ?>


<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="books">
USING XML DATA SOURCES IN JSP PAGES 5

<table border="1" width="100%">


<tr>
<th>ISBN</th>
<th>Title</th>
<th>Quantity</th>
<th>Price</th>
</tr>
<xsl:for-each select="book">
<tr>
<td>
<xsl:value-of select="@isbn"/>
</td>
<td>
<xsl:value-of select="title"/>
</td>
<td>
<xsl:value-of select="quantity"/>
</td>
<td>
<xsl:value-of select="price"/>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>

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.

Generating XML Using JSP Pages


In order for Web-based services to have the widest penetration possible it has
become increasingly important that they be accessible from the widest variety of
clients. Recent years have witnessed a proliferation of different types of user-ori-
ented clients for Web-based applications: PDAs, WAP-enabled mobile phones,
and landline voice clients. With the rise of business-to-business transactions,
GENERATING XML USING JSP PAGES 7

servers that consume XML can also be clients. The scenario we would like to
support looks like the following:

Clients Web Layer Data


Sources

Browser

PDA
XSLT Beans

Phone
(wireless,
JSP
landline)) Custom
Pages
Tags
Server

These clients speak the following languages:

Client Markup Language

PC-based browser HTML

PDA WML, XHTML

mobile phone WML, XHTML

landline phone VoiceXML

server application-specific XML languages

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

Generating XML From a JSP Page


We have already seen how JSP pages can consume XML data to generate
dynamic content. JSP pages can also generate XML data. Recall the XML docu-
ment introduced at the beginning of this paper:

<?xml version="1.0" encoding="ISO-8859-1"?>


<books>
<book isbn="123">
<title>Web Servers for Fun and Profit</title>
<quantity>10</quantity>
<price>$17.95</price>
</book>
...
</books>

If this information satisfies a business-to-business request for the contents of a


book warehouse, a JSP page could generate a response containing this docu-
ment. Furthermore, the URL XML_Book_Inventory in the consumer examples
described earlier could point to this JSP page.
The main requirement for generating XML is that the JSP page set the content
type of the page appropriately:

<%@ page contentType=”text/xml”%>


... XML document

Generating User-Oriented Markup Languages


There are two approaches to generating markup languages whose function is to
generate the user interface of an application:
• Single pipeline
• Multiple pipeline

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.

Clients Web Layer Data


Sources

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.

Clients Web Layer Data


Sources

HTML JSP Page

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:

Pipeline Development Runtime

Parse XML data


Single Client-specific XLST stylesheets Parse XSLT stylesheet
Apply XSLT transformation
CONCLUSION 11

Pipeline Development Runtime

Parse XML data


Server-side data abstractions Instantiate and initialize data
Multiple
Client-specific JSP pages abstraction components
Execute JSP page

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.

Clients Web Layer Data


Sources

HTML JSP Page

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

You might also like