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.
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.
Attributes: username, password, database, host, scope, id, dbtype, driver
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://...)
Closes the connection (if any). The id attribute is discussed above.
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.
Just like db:iferror, except that the content is executed only if no error occurred on the most recent database operation.
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.
Attributes: command, id
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.
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.
Attributes: column, htmlencode, id
Content: empty (should occur only in db:query tags)
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 &. 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).
Attributes: command, id
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.