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.4.5 A tag with attributes

Our HelloWorldTag is predictable; in fact, it always does exactly the same thing. In the dynamic world of web development, that is seldom the case, so let's look at a tag that behaves realistically, based on some user-specified attributes.

A web page might, for instance, need to display the value stored in a cookie such as a user name. Rather than forcing the page author to learn Java to access that value, we'll build a simple tag that does this for him. The tag should be flexible enough to be used in retrieving the value of any accessible cookie, so we'll create a tag attribute called cookieName to allow this. The first step in supporting this new attribute is to modify our tag handler class to receive and make use of this new attribute( listing 3. 6):

Listing 3. 6 Source code for the CookieValueTag handler class

The field that will get set by the attribute.

2. Prints the value of the cookie to the response.

Returns SKIP_ BODY to tell the JSP runtime to skip the body if one exists.

Invokes the set method when the JSP runtime encounters this attribute.

All we needed to do was add a set method called setCookieName() and assign a variable within it. The value of that variable is examined within our tag handler's doStartTag() to decide which cookie value to return. Now we need to inform the JSP runtime of this new tag and its attribute. Recall that the TLD is where we spec-ify this kind of information, so we need to modify our previous TLD to support CookieValueTag. The tag declaration in our TLD file (listing 3.7) now looks like the following:

Listing 3. 7 The new TLD file with our CookieValueTag
<? xml version=" 1.0" encoding=" ISO-8859-1" ?> 
<! DOCTYPE taglib 
	PUBLIC "-// Sun Microsystems, Inc.// DTD JSP Tag Library 1. 1// EN" 
	"http:// java. sun. com/ j2ee/ dtds/ web-jsptaglibrary_ 1_ 1.dtd"> 

<taglib> 
	<tlibversion> 1. 0</ tlibversion> 
	<jspversion> 1.1</ jspversion> 
	<shortname> simp</ shortname> 
	<uri> http:// www. manning. com/ jsptagsbook/ simple-taglib </ uri> 
	<info> 
		A simple sample tag library 
	</ info> 

	<tag> 
		<name> hello</ name> 
		<tagclass> book. simpletasks. HelloWorldTag</ tagclass> 
		<bodycontent> empty</ bodycontent> 
		<info> 
		Say hello. 
		</ info> 
	</ tag> 

	<tag> 
		<name> cookievalue</ name> 
		<tagclass> book. simpletasks. CookieValueTag</ tagclass> 
		<bodycontent> empty</ bodycontent> 
		<info> 
			Get a cookie's value. 
		</ info> 
		<attribute> 
			<name> cookiename</ name> 
			<required> true</ required>
		</ attribute> 
	</ tag> 
</ taglib> 

This tag will have an attribute called cookiename.

Specifies that this attribute is always required for this tag.

The tag definition itself should look familiar, since it is very similar to our Hello-WorldTag. The important difference is, of course, the attribute we've included. Note that the name of an attribute, in our case cookiename, is used by the JSP run-time to find setCookieName() to use in the tag handler; therefore, these need to match exactly for the tag to function.

To use this attribute within a JSP, syntax such as in listing 3. 8 works well:

Listing 3. 8 A JSP file to drive HelloWorldTag

Declares that the JSP file uses the library referenced by the URI and that the library's tags are referenced by jspx.

Uses the cookeivalue tag to retrieve a cookie called "username".

Assuming we've used this tag in a case where a cookie named "username" will be accessible, we'll see a message like that shown in figure 3.3.

Adding attributes to your tags makes them much more flexible and useful to the web pages where they are used. We explore the use of tag attributes in further detail in chapters 4 and 6.

3.4.6 Packaging tags for shipment

Once the tags have been tested to your satisfaction, it's time to package them in a standard deployable manner. Packaging tags means putting the implementation classes along with the library descriptor in a .jar file following a convention that further instructs you to:

  • Put your tag class files inside the .jar archive while maintaining their package structure. .
  • Put your TLD in the .jar file in a directory called META-INF.

For example, packaging our lone HelloWorldTag will require the following .jar file structure:

/book/ simpletasks/ HelloWorldTag. class 
/META-INF/ simpletags. tld 

This .jar packaging need not be complicated; all that's required is to create the desired directory structure on your file system and use the jar command (bundled with the JDK) to archive this structure into the .jar file. The command to place our class and TLD in a jar called hello. jar looks like this:

jar cf hello. jar META-INF book 

Now you can distribute your tag.

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.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
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%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

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

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs