advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement



Title: JSP Tag Libraries
ISBN: 193011009X
US Price: $35.96
Publication Date: May 2001
Pages: 656
© 2001 Manning Publications Co.

Reviews : Java Books :
JSP Tag Libraries : Chapter 3: Developing your first tags

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.

How to Add Java Applets to Your Site

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.

 Intel Go Parallel Portal
 Internet.com eBook Library
 IBM Software Construction Toolbox
 Microsoft RIA Development Center
 Destination .NET
XML error: not well-formed (invalid token) at line 43
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Google Hopes Chrome Will Help, Not Hurt Firefox
Remember Figlets? They're Back With Zend
Microsoft Readies an App Store Competitor?
Google: Chrome Browser Will Make Money
Sam Ramji: Microsoft's Man in Open Source
Google to Shake Up Browsers With Own Launch
Mozilla's Ubquity Mashup: For The Masses?
iPhone Users Just Want to Have Fun
Oops! I Fixed the Linux Kernel
Jim Zemlin: The New Center of Linux Gravity

Code Around C#'s Using Statement to Release Unmanaged Resources
Writing Functional Code with RDFa
BitLocker Brings Encryption to Windows Server 2008
Network Know-How: Exploring Network Algorithms
Create a Durable and Reliable WCF Service with MSMQ 4.0
The Baker's Dozen: 13 Tips for SQL Server 2008 and SSRS 2008
Book Excerpt: Microsoft Expression Blend Unleashed
Develop a Mobile RSS Feed the Easy Way
State of the Semantic Web: Know Where to Look
A 3D Exploration of the HTML Canvas Element

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Intel PDF: Virtualization Delivers Data Center Efficiency
Intel eBook: Managing the Evolving Data Center
Microsoft Article: BitLocker Brings Encryption to Windows Server 2008
Symantec eBook: The Guide to E-Mail Archiving and Management
Microsoft Article: RODCs Transform Branch Office Security
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
Avaya Article: Advancing the State of the Art in Customer Service
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Avaya Article: Avaya AE Services Provide Rapid Telephony Integration with Facebook
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Seminar: Efficiencies in Hardware/Software Virtualization
HP Webcast: Disaster Recovery Planning
Go Parallel Video: Performance and Threading Tools for Game Developers
HP Video: StorageWorks EVA4400 and Oracle
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
IBM TCO eKIT: Your IT Budget is Under Attack, Get in Control
IBM Energy Efficiency eKIT: Learn How to Reduce Costs
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt and free High-Performance SQL Code eBook
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
Microsoft Article: Silverlight Streaming--Free Video Hosting for All
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
HP Demo: StorageWorks EVA4400
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES