3.1 What are JSP custom tags?
At its most fundamental level, a tag is a group of characters read by a program for
the purpose of instructing the program to perform an action. In the case of HTML
tags, the program reading the tags is a Web browser, and the actions range from
painting words or objects on the screen to creating forms for data collection. Cus-tom
JSP tags are also interpreted by a program; but, unlike HTML, JSP tags are
interpreted on the server side— not client side. The program that interprets custom JSP
tags is the runtime engine in your application server (TomCat, JRun,
WebLogic, etc.). When the JSP engine encounters a custom tag, it executes Java
code that has been specified to go with that tag. Common tasks performed by tag
codes include retrieving database values, formatting text, and returning HTML to a
browser. Since a tag references some Java code to run when it's encountered, one
way to think of a tag is simply as a shorthand notation for a block of code.
Notice in figure 3.1 that when the JSP runtime encounters the tag, it causes a
block of Java code to execute and return a message to the client's browser.
3.1.1 Anatomy of a tag
Tags are often structured with a body and/ or attributes which are the places where a
page author (the user of the tag) can include more information about how the tag
should do its job. The following snippet shows the general structure of a tag.
<tagname attributename=" attributevalue"
otherattributename=" otherattributevalue">
Tag's body... can contain about anything.
</ tagname>
This syntax should look familiar, since we see it so often in HTML tags, such as:
<font face=" Tahoma" size= 3">
Tag, you're it!
</ font>
Tags can also appear without a body, meaning that the start tag does not have a
matching end tag. These "bodyless" tags look like this:
<bodylesstagname attributename=" attributevalue"
otherattributename=" otherattributevalue"/>
You've probably seen examples of bodyless tags in HTML, such as:
<input type=" input" name=" body">
Bodyless tags usually represent a certain function, as in the printing of the value of a
database field onto the page. Tags often have bodies in order to perform an opera-tion
on the content in the body, such as formatting, translating, or processing it in
some way.
JSP custom tags are merely Java classes that implement one of two special inter-faces.
Since tags are standard Java classes, they can interact with, delegate to, or
integrate with any other Java code in order to make that functionality available
through a tag. For instance, we might have a library of utility classes we've written
for composing and sending email, or for accessing a particular database that we'd
like to make available to HTML developers. We need build only a few tags that col-lect
the necessary information through attributes and pass this information to our
utility classes.
3.1.2 Using a tag in JSP
JSP code that uses email and database tags such as those just mentioned might look
something like this:
<html>
I am sending you an email with your account information
<jspx: sendmail server=" mail. corp. com"
from=" john. doe@ corp. com"
to=" foo@ bar. com"
subject=" mail from a tag">
Look at how easy it is to send an email from a tag... here is
your status.
<jspx: dbaccess>
<jspx: wdbcon id=" con1"/>
<jspx: wjitdbquery>
select reserves from account where id= '<%= userid %> '
</ jspx: wjitdbquery>
You have <jspx: wdbshow field=" reserves "/>$ in your saving account.
</ jspx: dbaccess>
</ jspx: sendmail>
</ html>
Among the JSP and HTML fragments are special tags prefixed with jspx. Even to
the untrained eye, these tags appear to query a database, present the information in
the content of an email, and send the message. Notice how the attributes help
gather information such as the email sender and subject and the field in the data-base
to display. Also, note how the <jspx: wjitdbquery> tag contains a Structured
Query Language (SQL) statement within its body that it uses for the database query.
This is a good example of what a JSP using custom tags might look like. Consider
how much messier this JSP would look if we had to include all the Java code neces-sary
for creating classes, setting properties, catching exceptions, and so forth.
3.1.3 The tag library descriptor
An important step in creating tags is specifying how they will be used by the JSP
runtime that executes them. To properly work with a tag, the runtime must know
several things about it, such as what (if any) attributes it has, and whether or not it
has a body. This information is used by the runtime to verify that the tag is properly
employed by a JSP author and to correctly execute the tag during a request. This
crucial information is made available to the runtime engine via a standard XML file
called a tag library descriptor (TLD), a key component of the JSP Specification and
standard across all products that implement it. How to create a TLD is discussed in
section 3.2.4, and covered in greater detail in chapter 5 and appendix B.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|