10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Programming SimpleTag Handlers<br />

262<br />

set, the handler retrieves the connection object from the page context. Otherwise, the tag<br />

handler first retrieves the tag handler for the enclosing tag and then retrieves the connection<br />

object from that handler.<br />

public class QueryTag extends SimpleTagSupport {<br />

public int doTag() throws JspException {<br />

String cid = getConnectionId();<br />

Connection connection;<br />

if (cid != null) {<br />

// there is a connection id, use it<br />

connection =(Connection)pageContext.<br />

getAttribute(cid);<br />

} else {<br />

ConnectionTag ancestorTag =<br />

(ConnectionTag)findAncestorWithClass(this,<br />

ConnectionTag.class);<br />

if (ancestorTag == null) {<br />

throw new JspTagException("A query without<br />

a connection attribute must be nested<br />

within a connection tag.");<br />

}<br />

connection = ancestorTag.getConnection();<br />

...<br />

}<br />

}<br />

}<br />

<strong>The</strong> query tag implemented by this tag handler can be used in either of the following ways:<br />

<br />

...<br />

<br />

<br />

SELECT account, balance FROM acct_table<br />

where customer_number = ?<br />

<br />

<br />

<br />

<br />

SELECT account, balance FROM acct_table<br />

where customer_number = ?<br />

<br />

<br />

<br />

<strong>The</strong> TLD for the tag handler uses the following declaration to indicate that the connectionId<br />

attribute is optional:<br />

<strong>The</strong> <strong>Java</strong> <strong>EE</strong> 5<strong>Tutorial</strong> • June 2010

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!