CPSC 371 Lab, 7 November 2003
A Database Taglib

A common use for custom JSP tags is database access. Using custom tags can move a lot of messy Java code off the page. As an example, I have written a fairly simple "xdb" taglib for database access. This file documents the tags that are defined by that taglib. The complete java source code and the tld file are also available.

The ConnectionInfo Object and the "id" Attribute

When this taglib is used to create a database connection, all the data about the connection is stored in an object of type xdb.ConnectionInfo. This object, in turn, is stored on one of the scope objects, pageContext, request, session, or application. (The scope can be specified in the db:connect tag.) The key value that identifies the attribute is, by default, "__xdbtags_connection_object__". You can use this tag to retrieve the ConnectionInfo object for use in Java code.

Every tag in this taglib has an optional attribute named id. This can be used to specify an alternate attribute key value for the ConnectionInfo object. The id in the db:connect tag sets the key under which the object is stored. If a non-default id is used, the same id must be specified in every tag that refers to the same connection. For the most part, you should not use the id attribute.

The "db:connect" Tag

Attributes: username, password, database, host, scope, id, dbtype, driver
Content: empty
Example: <db:connect username="admin" password="fred" database="users" scope="session" />

This tag opens a connection to a database server, and saves info about the connection so that it can be used by other tags in the taglib. You should always open a connection before using any of the other tags in this taglib.

Use the username, password, database, and host to specify the usual access information for the connection (database gives the name of the database that you want to use). The default values are "root", "", "", and "localhost", respectively. The value of the scope can be "page", "request", "session", and "application" and specifies which scope the connection information is to be stored in. The default is "page" scope. The id attribute is discussed above.

If you are using a mysql database with the standard driver, you can ignore the dbtype and driver; the alternative is not discussed here. The default values are "mysql" and "org.gjt.mm.mysql.Driver." (The dbtype is used to form the JDBC URL, jdbc:mysql://...)

The "db:close" Tag

Attributes: id
Content: empty
Example: <db:close/>

Closes the connection (if any). The id attribute is discussed above.

The "db:iferror" Tag

Attributes: id
Content: JSP
Example: <db:iferror>
               Sorry, <db:errormessage>
       </db:iferror>

The db tags do not crash when a database error occurs; they just quietly do nothing. You can use the db:iferror and db:ifnoerror tags to check whether an error has occurred. The content of the db:iferror tag is executed only if the most recent database tag generated an error. If no error has occurred, the content is skipped.

The "db:ifnoerror" Tag

Attributes: id
Content: JSP

Just like db:iferror, except that the content is executed only if no error occurred on the most recent database operation.

The "db:errormessage" Tag

Attributes: id
Content: empty
Example: See <db:iferror>

If an error occurred on the most recent database operation, this will output an error message to the page. If no error has occurred, no output is generated.

The "db:update" Tag

Attributes: command, id
Content: empty
Example: <db:update command="DELETE FROM users WHERE userid=17"/>
Example: <db:update command="<%= updatecommand %>"/>

Executes an SQL update command on the open connection. The command attribute, which is mandatory, gives the SQL command. Note that the command can be given by a run-time expression.

The "db:query" Tag

Attributes: command, id
Content: JSP (can include db:get tags)
Example: <db:query command="SELECT * FROM users"/>
               <p>User <db:get column="name"/> has password <db:get column="password"/></p>        </db:query>

Executes an SQL query command on the open connection. The command, which is mandatory, gives the SQL command. Note that the command can be given by a run-time expression.

This is a tag that "reiterates" its body. The body of the tag is executed once for each row of the result set returned by the query. The items in the row can be retrieved using the db:get command. The example actually generates a complete list of all the user names in the table along with their passwords.

The "db:get" Tag

Attributes: column, htmlencode, id
Content: empty (should occur only in db:query tags)
Example: <db:/>

For use in the body of a db:query tag. This fetches one value from the current row and outputs its value to the page. The column attribute, which is mandatory, tells which column to fetch the value from. It can be given either as a column number (with numbering starting from 1) or as a column name. By default, all the outputs are passed through an encoder that changes special html characters such as &: and < to their entity codes, such as &amp;. If you provide a value of "false" for the htmlencode attribute, this is not done (so that any html code in the output will be interpreted by the browser).

The "db:table" Tag

Attributes: command, id
Content: empty
Example: <db:table/>

This convenience tag does an SQL query, just like db:query, but it produces a centered, formated table of the result set automatically, including column names and row numbers.


David Eck