JSP.A.2 A Set of SQL Tags


The following is a possible set of SQL tags. Note that this specific syntax is only used for pedagogical reasons, no endorsement is implied .

JSP.A.2.1 connection , userid , and password

The connection tag creates a connection using some userid and password information. To show tag communication, userid and password are actually subelements of connection .

 <x:connection id="con01"      ref="connection.xml">    <x:userid><%=session.getUserid()%></x:userid>    <x:password><%=session.getPassword()%><x:password>  </x:connection> 

In this example the "con01" object is available after the element.

This example uses the runtime stack so userid and password can locate their enclosing connection tag and can update userid and password data in there. This example also uses PageContext to register the SQL connection object with the page context using "con01" so it can be retrieved later.

Tag Library Descriptor

Indicates the names of the tags and their attributes. It associates Tag handlers with the tags. It also associates the ConnectionExtraInfo as the TagExtraInfo for connection.

UserIdTag

UserIdTag needs access to its body; this it can do by defining a doAfterBody() method. This method will take the BodyContent and convert it into a String . Then it will use the findAncestorWithClass() static method on Tag to locate the enclosing connection tag instance and will set the desired userid information on that object.

PasswordTag

This Tag handler is equivalent to UserIdTag .

ConnectionTag

This Tag handler provides methods to setUserId() and to setPassword() that will be used by the enclosed actions; it also provides a getConnection() method that on-demand computes the desired SQL connection. This tag handler needs not be concerned with the body computation, but it will need to register the SQL connection object with the pageContext object if an ID is provided.

ConnectionExtraInfo

This class is identical to BarExtraInfo from a previous example.

JSP.A.2.2 Query

The connection can now be used to make a query. The query element takes the body of the tag and makes a query on it. The result gets inserted in place.

 <x:query id="balances" connection="con01">     SELECT account, balance FROM acct_table     where customer_number = <%= request.getCustno()%>  </x:query> 

The implementation highlights are:

Tag Library Descriptor

Query has two mandatory attributes (in our example), and they are described as so in the TLD. The TLD also associates QueryTag as the Tag handler class, and QueryExtraInfo as the TagExtraInfo for the query tag.

QueryTag

QueryTag needs access to its body; this it can do by defining a doAfterBody() method. This method will take the BodyContent and convert it into a String. Then it will use the PageContext object to locate an SQL connection that was registered using the id that is the value to the connection attribute. The result of the query will be registered in the PageContext with the value of the id attribute as its name .

QueryExtraInfo

This class is identical to BarExtraInfo from a previous example.

JSP.A.2.3 Iteration

Finally the query result can later be used to present the data by dynamically creating a series of <li> elements.

 <ul>  <x:foreach iterate="row" in="balances">  <li>The balance for  account <%= row.getAccount()%> is <%= row.getBalance()%>  </x:foreach>  </ul> 

Unlike query and connection , the foreach element does not define a new object for later use but it defines (and redefines) a "row" object that is accessible within its start and end tags.

The implementation of this tag requires the repeated evaluation of the body of the tag.

Tag Library Descriptor

foreach has two mandatory attributes (in our example), and they are described as so in the TLD. The TLD also associates ForEachTag as the Tag handler class, and ForEachExtraInfo as the TagExtraInfo for the foreach tag.

ForEachTag.doStartTag()

ForEachTag needs to define a doStartTag() method to extract the value of the in and iterate attributes from the attribute values. The value of in ("balances" in this example) is used to get at the result data. The value of iterate ("row" in this example) is used as the key on which to store the iteration value.

The current value of the "out" variable is stored away so it can be used in doBody() . This method returns EVAL_BODY so as to force the evaluation of the body.

ForEachTag.doAfterBody()

The BodyContent (obtained from getBodyContent() ) contains the evaluation of the body of the element where the evaluation has been done in a context where the variable "row" is assigned the different rows of the query. This method now inserts this content into the surrounding out stream (obtained from getPreviousOut() ).

This method now updates the binding of "row" and will return EVAL_BODY or SKIP_BODY depending on whether there were any more rows in the result set.

ForEachTag.doEndTag()

Just clean up.

ForEachExtraInfo()

The translation-time information indicates that this action defines a new scripting variable, with scope NESTED and name corresponding to the value of the "row" attribute.



Java 2 Platform, Enterprise Edition. Platform and Component Specifications
Java 2 Platform, Enterprise Edition: Platform and Component Specifications
ISBN: 0201704560
EAN: 2147483647
Year: 2000
Pages: 399

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net