ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

JSP Standard Tag Libraries, Part 2
Pages: 1, 2, 3

SQL Actions

The SQL actions allow for direct access to data sources. That statement alone should set off red flags in your MVC architecture; I myself am thoroughly against using these tags in production applications. While they might serve a purpose for RAD, prototyping, or very simple applications, they certainly should not be used in large-scale application development. It seems like there are a lot of developers out there who really wanted this functionality to be available to them, so it's in the spec; therein lies the beauty of the JCP. With that said, let's look at what the SQL actions can provide.



Using these tags makes it possible to set datasources, query the datasource, have easy access to result sets, and perform updates as well as transactions. All of the SQL tags act upon a datasource. The datasource can be provided in a number of ways: it can be configured via the configuration parameter sql.datasource, there can be an Object that is provided by the application logic, or there can be an Object provided by the <sql:setDataSource> action. For example, if I were using a MySql database with the JDBC driver, I can define the datasource as:


<sql:setDataSource var="datasource"  driver="org.gjt.mm.mysql.driver" url="jdbc:mysql://localhost/db" />

For those familiar with the JDBC DriverManager, <sql:setDataSource> is just a wrapper around that facility. If the datasource attribute is a String, it could either be a relative path to a JNDI resource or a JDBC parameter string. A query can then access the datasource like so: <sql:query datasource="${datasource}" ... />

If we put a whole sample together to execute a query and then display the result set, it might look like:


<sql:query var="customer" datasource="${datasource}" 
SELECT * FROM customers WHERE state = 'CO' ORDER BY city
</sql:query>
<table>
<c:forEach var="row" items="${customers.row}">
	<tr>
	<td><c:out value="${row.custName}" /></td>
	<td><c:out value="${row.address}" /></td>
	</tr>
</c:forEach>
</table>

Performing updates and transactions is just as simple. For example, we can define a transaction and then any number of updates to be performed in that transaction. This is shown in the sample below.


<sql:transaction dataSource="${dataSource}">
<sql:update>
	UPDATE account SET Balance =Balance -? WHERE accountNo = ?
	<sql:param value="${transferAmount}"/>
	<sql:param value="${accountFrom}"/>
</sql:update>
</sql:transaction>

The <sql:dateParam> action can be used to set the values of parameter markers (?) in a SQL statement, for values of type java.util.Date. While not shown in this example, it is possible to specify the isolation level of the transaction. Transaction isolation levels are the ones defined by java.sql.Connection. If no transaction level is specified, the isolation level the datasource has been configured with is used. Any SQLException that might occur in the course of the transaction is caught and rethrown as a JspTagException by the tag.

XML Actions

The last set of actions to look at is the XML actions. The XML actions can be further categorized into core, flow control, and transform actions. The XML set of actions in the JSTL is based on Xpath, which is used as the local expression language for the XML actions. The attribute select is used to specify the XPath expression for all of the XML actions of JSTL. This is a string literal that is evaluated the XPath engine.

The XML core actions are similar to those provided in the Core actions. Actions include <x:out>, <x:set>, and <x:parse>. The main difference between the Core tags and the XML tags is that the XML tags support XPath expressions, while the Core tags don't. The parse tag provides functionality to parse an XML document into a data structure. This data structure can then be processed by the XPath engine. So, for example, if we have an XML file that identifies a book, we can parse it, and then access that data using an XPath expression and print it out:


<c:import url="http://oreilly.com/book?id=1234" var="xml"/>
<x:parse source="${xml}" var="bookInfo"/>
<x:out select="$bookInfo/title"/>
<x:out select="$bookInfo/author"/>

The XML Flow control actions are the same as in Core. They include: if, choose, when, otherwise, and forEach tags. The only difference is that the select attribute is used to specify the XPath expressions. The expression is evaluated, and the resulting object is converted to a boolean according to the semantics of the XPath boolean() function. These semantics are:

  • A number is true if and only if it is neither positive or negative zero nor NaN.
  • A node-set is true if and only if it is non-empty.
  • A string is true if and only if its length is non-zero.

The XML transform actions are used to take an XML document and apply an XSL stylesheet to it. The result of the transformation is written to the page, but it's possible to capture the result in an Object specified via the result attribute, or save it in a scope using the var and scope attributes. Performing a transformation is as simple as importing the XML and XSL files and then doing a transform:


<c:import url="/books" var="xml"/>
<c:import url="/WEB-INF/xslt/bookDisplay.xsl" var="xslt"/>
<x:transform source="${xml}" xslt="${xslt}"/>

If you need to specify parameters to the transformation, use the <x:param> action to indicate the parameter name and value.

Summary

You have learned how to use some of the cool tags being provided in the JSTL. Surely, the implementation of your JSP files will become easier and faster using the actions that are provided. Keep up on what's going on with the JSTL; when the final draft is available, we should also see vendors optimizing their JSP containers for handling JSTL tags. If you want to start playing with the reference implementation, download the latest version.

Sue Spielman is an associate editor for ONJava.com, covering JSP and Servlets technologies. She is also President and Senior Consulting Engineer for Switchback Software LLC.


Read more JSP and Servlets columns.

Return to ONJava.com.