Displaying Database Data
Here's how to retrieve and display the example
Catalog database table in a JSP. Create a new JSP called
DisplayData.jsp. In it, specify a
taglib directive for the XTags tag library:
<%@ taglib uri="http://jakarta.apache.org/taglibs/xtags-1.0"
prefix="xtags" %>
Import the XSU and DOM4J classes:
<%@ page import=
"oracle.xml.sql.query.OracleXMLQuery,
oracle.xml.sql.OracleXMLSQLException,
oracle.xml.sql.OracleXMLSQLNoRowsException,
org.dom4j.*,org.dom4j.io.*,
java.sql.*,
java.io.*,
org.w3c.dom.*,
javax.naming.InitialContext" %>
Obtain a JDBC connection from the datasource JNDI "OracleDS" you configured for the WebLogic server:
InitialContext ctx=new InitialContext();
javax.sql.DataSource ds=(
javax.sql.DataSource)ctx.lookup("OracleDS");
Connection connection=ds.getConnection();
Create an OracleXMLQuery object from the database table using a SQL query:
OracleXMLQuery query = new OracleXMLQuery(
connection, "SELECT CatalogId, " +
"JOURNAL, PUBLISHER, EDITION, TITLE," +
"AUTHOR FROM OE.CATALOG");
Set the row tag for the XML document generated with XSU. This creates an XML document row tag for each row in the database table:
query.setRowTag("CATALOG");
Obtain an org.w3c.dom.Document object from the OracleXMLQuery object:
org.w3c.dom.Document domDocument=query.getXMLDOM();
Convert the org.w3c.dom.Document object to a org.dom4j.Document object:
DOMReader domReader=new DOMReader();
org.dom4j.Document dom4jDocument=domReader.read(domDocument);
Output the org.dom4j.Document object to an XML file:
XMLWriter output=new XMLWriter(
new FileWriter(
"C:/BEA/weblogic81/samples/server/examples/" +
"build/mainWebApp/catalog.xml"));
output.write(dom4jDocument);
Parse the XML document output with the XTags tag
xtags:parse:
<xtags:parse uri="catalog.xml"/>
The XPath and XSLT tags in the JSP use the XML document parsed with the
xtags:parse tag. Add an HTML table and a header row for the HTML table, and for each row in the XML document, add a corresponding HTML row to the table. You use the
xtags:forEach tag to iterate over the "rows" in an XML document. The
xtags:valueOf tag is used to obtain the value of an element in a XML document. Iterate over the XML document row tags and add rows to the HTML table.
 | |
Figure 1. Database Data Converted with XTags: Here's how the database table looks when displayed in a browser after the XTags conversion. |
<xtags:forEach select="/ROWSET/CATALOG">
<tr>
<td><xtags:valueOf select="CATALOGID"/></td>
<td><xtags:valueOf select="JOURNAL"/></td>
<td><xtags:valueOf select="PUBLISHER"/></td>
<td><xtags:valueOf select="EDITION"/></td>
<td><xtags:valueOf select="TITLE"/></td>
<td><xtags:valueOf select="AUTHOR"/></td>
</tr>
</xtags:forEach>
Listing 1 shows the complete code in
DisplayData.jsp.
Finally, copy the completed
DisplayData.jsp file to the
C:/BEA/weblogic81/samples/server/examples/build/mainWebApp directory. Now, if you browse to the file using the URL:
http://localhost:7001/DisplayData.jsp, you'll see the database table displayed in the JSP as shown in
Figure 1.
Once you get past the rather lengthy setup, you'll find that using the XTags tag library makes it much easier to use XSLT functionality in your JSPs.