230 likes | 386 Views
Java, XML and WAP. Wei Gu. Outline and Scope. Generate WML content using JSP and XML Use “Ride Board” application as an example, discuss software architecture Discuss two approaches of implementing a general-purpose output component that wraps data into WML and other formats: JSP XSLT.
E N D
Java, XML and WAP Wei Gu
Outline and Scope • Generate WML content using JSP and XML • Use “Ride Board” application as an example, discuss software architecture • Discuss two approaches of implementing a general-purpose output component that wraps data into WML and other formats: • JSP • XSLT
XML and Java Technology • Complementary technologies • XML is portable reusable data • Java Technology is portable behavior • Both are Web-friendly, open, vendor neutral, Unicode support
Ride Board Application (2) dbCommand(Request) Main JSP Page Main Bean (1) Request (4) Whereto(target) Configure JSP Page ENTRY Query Form (3) Create DB Handler (5) forward(Request) Output JSP Page (8) Response (6) Run Query Database Query Result DB Handler (7)QueryResult Query form
Software Architecture • 3-tier application • Robust database component(provides security and connection pooling) • A main JSP page function as a dispatcher • Dedicated JSP pages for output • Business logic and Java Code wrapped into Bean and helper Java classes • Generic framework that is flexible to change
findByZip.jsp <%@ page contentType="text/vnd.wap.wml;charset=ISO-8859-1" %> <?xml version="1.0"?> <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml"> <wml> <%@ page errorPage="../wmlerrorpage.jsp" %> <jsp:useBean id="qBean" scope="session" class="MyNa.jspUtil.QBean" /> <head> <meta http-equiv="Cache-Control" content="no-cache" forua="true"/> </head> <card id="output" title="findByZip"> <do type="accept" label="again" > <go href="#start" /> </do>
findByZip.jsp(cont.) <% MyNa.jspUtil.QueryResult qR=qBean.queryResult(); String[][]rows=(null==qR)?null:qR.getRows(); if(null==rows || rows.length<1){ %> <p>Sorry, no rides to your zip-code.</p> <% }else{ String[]headers=qR.getColumnHeaders(); %> <p> <table columns="2"> <% // we use only rows[0] to generate a 2-column table of fields for(int j=0;j<headers.length;j++){ %>
findByZip(cont.) <tr> <td><%= headers[j] %></td><td><%= rows[0][j] %></td> </tr> <% } %> </table> </p> <% } %> </card> <%@ include file="ridesInc.jsp" %> </wml>
XSLT • A Language used for writing stylesheets that transform XML document to other formats • XSLT processor: XT from James Clark • XML Parser: Sun’s Java API for XML Parsing(JAXP), a parser based on SAX • JDK 1.2 or later required
Ride Board Revisited (2) dbCommand(Request) Main JSP Page Main Bean (1) Request (4) Whereto(target) Configure JSP Page ENTRY Query Form (7) Query (5) forward(Request) (6) Query Servlet / JSP (9) Response Database Transformer DB Handler (8)QueryResult Query Result Output
Two Approaches • Static Way • Run Query and Save the result as an XML file • Run XSLT stylesheet on that file • Dynamic Way • Run query from within an XSLT stylesheet • Running a stylesheet on an object in memory that is not an XML stream
Approach I Static Way • Problem to Solve • Make XT processor understand a special content-type: text/vnd.wap.wml • Solution: • Modify the XT package Change DocumentHandler class
Rows.xml <table> <row> <field name=‘rowId’>1</field> <field name=‘FromZip’>11111</field> <field name=‘ToZip’>22222</field> <field name=‘Day’>2/20/2000</field> </row> <row> <field name=‘rowId’>2</field> <field name=‘FromZip’>33333</field> <field name=‘ToZip’>44444</field> <field name=‘Day’>3/15/2000</field> </row> </table>
findByZipFile.xsl(XHTML) <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/TR/xhtml1/strict" exclude-result-prefixes="#default" > <xsl:output method="html" indent="yes" /> <xsl:template match="/"> <html> <head><title>Rides from the table</title></head> <body> <xsl:variable name="rows" select="table/row" />
findByZipFile.xsl(cont.) <xsl:choose> <xsl:when test="count($rows)=0"> Sorry, no rides for your zip code destination. </xsl:when> <xsl:otherwise> <table border="1"> <tr> <xsl:for-each select="$rows[1]/field"> <th> <xsl:value-of select="@name" /> </th> </xsl:for-each> </tr> <xsl:for-each select="$rows"> <tr>
findByZipFile.xsl(cont.) <xsl:for-each select="*" > <td> <xsl:value-of select="." /> </td> </xsl:for-each> </tr> </xsl:for-each> </table> </xsl:otherwise> </xsl:choose> </body></html> </xsl:template> </xsl:stylesheet>
findByZipFile.xsl (wml) <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" <!– new namespace for Java code that does WML output--> xmlns:javaout="http://www.jclark.com/xt/java" exclude-result-prefixes=“javaout“ <!—Don’t show namespace on output--> > <xsl:output <!—custom output element for WML--> method=“javaout:MyNa.jspUtil.XMLOutputHandler" indent="yes" encoding = “UTF-8” media-type = “text/vnd.wap.wml” <!—becomes content-type --> omit-xml-declaration=“no” doctype-public=“-//WAPFORUM//DTD WML 1.1//EN” doctype-system = “http://www.wapforum.org/DTD/wml_1.1.xml” />
Approach II Dynamic Way • Problem to Solve • Fed the query result into the XML parser for XSLT processing • Solution: • Construct the InputSource class from StringReader to read xmlString from memory
Feeding a NodeIterator into XT • Declaration <xsl:stylesheet version=“1.0” …… xmlns:xt = “http://www.jclark.com/xt” xmlns:xqd = “http://www.jclark.com/xt/java/MyNa.jspUtil.XmlQueryStringDoc” exclude-result-prefixes = “javaout xqd” > • Initialize ‘rows’ variable by extension-function call <xsl:variable name=“tree-frag”> <xsl:copy-of select=“xt:node-set(xqd:query-result($theSessionID))” /> </xsl:variable> <xsl:variable name=“rows” select=“xt:node-set($tree-frag)//row” />
Summary • Servlets and JSP • Architecture for JSP application • Generating WML (and other XML) content from database queries using JSP • Channeling query output through the XSLT processor • Generating WML (and other XML) from the same data source using XSLT for output
References • Professional WAP Charpter 10, Alex Nakhimovsky et al., Wrox Press, 2000 • Professional XML, Didier Martin, Mark Birbeck, Michael Kay et al., Wrox Press, 2000 • Design Patterns, Gamma et al., Addison-Wesley, 1995 • http://www.jclark.com/xml • http://java.sun.com/products/jsp • http://jakarta.apache.org/