Skip navigation.

Programming WebLogic JSP Tag Extensions

  Previous Next vertical dots separating previous/next from contents/index/pdf Contents View as PDF   Get Adobe Reader

Creating a Tag Library Descriptor

The following sections describe how to create a tag library descriptor (TLD):


Overview of Tag Library Descriptors

A tag library allows a developer to group together tags with related functionality. A tag library uses a tag library descriptor (TLD) file that describes the tag extensions and relates them to their Java classes. WebLogic Server and some authoring tools use the TLD to get information about the extensions. TLD files have the file extension .tld and are written in XML notation.


Writing the Tag Library Descriptor

Order the elements in the tag library descriptor file as they are defined in the XSD. This ordering is used in the following procedure. The XML parser throws an exception if you incorrectly order the TLD elements.

The body of the TLD contains additional nested elements inside of the <taglib> ... </taglib> element. These nested elements are also described in the steps below. For display in this document, nested elements are indented from their parent elements, but indenting is not required in the TLD.

The example in Sample Tag Library Descriptor declares a new tag called code. The functionality of this tag is implemented by the Java class weblogic.taglib.quote.CodeTag.

To create a TLD:

  1. Create a text file with an appropriate name and the extension .tld, and save it in the WEB-INF directory of the Web application containing your JSP(s). Content beneath the WEB-INF directory is non-public and is not served over HTTP by WebLogic Server.
  2. Include the following header:
  3. <taglib version="2.0" xmlns="" xmlns:xsi="" xsi:schemaLocation="">
  4. Add the contents of the TLD, embedded in a <taglib> element, as indicated in steps 4-7. The contents include elements containing information about the tag library and elements that define each tag. For example:
  5. <taglib>
    ... body of taglib descriptor ...
  6. Identify the tag library:


(Required) The version number of the tag library.


(Required) Describes the JSP specification version (number) this tag library requires in order to function. The default is 2.0.


(Required) Assigns a short name to this tag library. This element is not used by WebLogic Server.


(Required) Defines a public URI that uniquely identifies this version of the tag library.


(Optional) Contains a short name that is intended to be displayed by tools.



(Optional) Contains the name of a file containing a small (16 x 16) icon image. The file name is a relative path within the tag library. The image must be either in the JPEG or GIF format, and the file name must end with the suffix ".jpg" or ".gif" respectively. The icon can be used by tools.


(Optional) Contains the name of a file containing a large (32 x 32) icon image. The file name is a relative path within the tag library. The image must be either in the JPEG or GIF format, and the file name must end with the suffix ".jpg" or ".gif" respectively. The icon can be used by tools.



(Required) Defines an arbitrary text string describing the tag library.


(Optional) Provides information on the scripting variables defined by this tag. A translation-time error occurs if a tag that has one or more variable subelements has a TagExtraInfo class that returns a non-null object.


(Optional) Defines an optional event listener object to be instantiated and registered automatically.

  1. Define a tag library validator (Optional).


Top level element for a validator.


(Required) The Java class that performs the validation.


(Optional) Defines initialization parameters for the validator class.


Defines the name of this parameter.


Defines the value of this parameter.

  1. Define a tag.
  2. Use a separate <tag> element to define each new tag in the tag library. The <tag> element takes the following nested tags:


(Required) Defines the name of the tag. This is used when referencing the tag in a JSP file, after the ":" symbol, For example:


For more information, see "WebLogic Server Known and Resolved Issues" lists known problems for WebLogic Server..


(Required) Declares the tag handler class that implements the functionality of this tag. Specify the fully qualified package name of the class.

Locate the class file under the WEB-INF/classes directory, in a directory structure reflecting the package name. You can also package the classes in a tag library jar file; for more information, see Packaging a JSP Tag Library as JAR File.


(Optional) Declares the subclass of TagExtraInfo that describes the scripting variables introduced by this tag. If your tag does not define new scripting variables, it does not use this element. Specify the fully qualified package name of the class. You can perform validation of the tag's attributes in this class.

Place the class files under the WEB-INF/classes directory of your Web application, under a directory structure reflecting the package name. You can also package the classes in a tag library jar file; for more information, see Packaging a JSP Tag Library as JAR File.

<body-content>empty | JSP | scriptless | tagdependent</body-content>

(Optional) Defines the content of the tag body.

empty means that you use the tag in the empty tag format in the JSP page. For example: <taglib:tagname/>

JSP means that the contents of the tag can be interpreted as a JSP and that you must use the tag in the body tag format. For example:


scriptless means that the contents of the tag do not contain any scripts or scripting elements.

tagdependent means that your tag will interpret the contents of the body as a non-JSP (for example, an SQL statement).


(Optional) Defines the name of the attribute as it appears in the tag element in the JSP page. For example:

<taglib:mytag myAttribute="myAttributeValue">

Use a separate <attribute> element to define each attribute that the tag can take. Tag attributes allow the JSP author to alter the behavior of your tags.


   <required>true | false</required>

(Optional) Defines whether this attribute has optional use in the JSP page.

If not defined here, the default is false — that is, the attribute is optional by default.

If true is specified, and the attribute is not used in a JSP page, a translation-time error occurs.

   <rtexprvalue>true | false</rtexprvalue>

(Optional) Defines whether this attribute can take a scriptlet expression as a value, allowing it to be dynamically calculated at request time.

If this element is not specified, the value is presumed to be false.


  1. Define scripting variables (optional).
  2. Within the <tag> element, you can define scripting variables.


Top level element for declaring a variable.


Defines the name of the variable, or you can define the name from an attribute using.


Names the variable with the value of attrName.


The Java type of this variable.


(Optional) If set to true, indicates that the variable is to be defined.


The scope of the scripting variable. Valid options are:

NESTED (The variable is only available inside the tag body)

AT_BEGIN (The variable is defined just before executing the body)

AT_END (The variable is defined just after executing the body.)



Sample Tag Library Descriptor

The following is a sample listing of a tag library descriptor.

Listing 3-1 Sample Tag Library Descriptor (tld)

<taglib version="2.0" xmlns="" xmlns:xsi="" xsi:schemaLocation="">
<uri>tag lib version id</uri>
This tag library contains several tag extensions
useful for formatting content for HTML.


The DynamicAttributes Interface

The DynamicAttributes interface, supported by the JSP 2.0 container, allows you to use tags with values that are treated in a consistent manner but with names that are not necessarily known at development time. For example, if you want to customize the width portion of the <table> HTML tag by determining its value at runtime, you could make use of the DynamicAttributes interface to customize only the portion of the tag you want to change, without needing to specify anything about the other portions of the <table> tag (border, cellspacing, and so on). Without the DynamicAttributes interface, you would have needed to write a Tag Handler or other complicated code to accomplish the task of determining the value of width dynamically at runtime.


Tag Handler Support of Dynamic Attributes

The TLD is what ultimately determines whether a tag handler accepts dynamic attributes or not. If a tag handler declares that it supports dynamic attributes in the TLD but it does not implement the DynamicAttributes interface, the tag handler must be considered invalid by the container.

If the dynamic-attributes element (a child element to the tag element for the tag library being authored) for a tag being invoked contains the value true, the following requirements apply:

Dynamic Attributes Example

In the following example, assume attributes a and b are declared using the attribute element in the TLD, attributes d1 and d2 are not declared, and the dynamic-attributes element is set to true. You set the attributes using the calls:

Listing 3-2 Dynamic Attribute Example

<jsp:root xmlns:mytag="" version="2.0">
<mytag:invokeDynamic a="1" d1="2" mytag:d2="3">
<jsp:attribute name="b">4</jsp:attribute>
<jsp:attribute name="d3">5</jsp:attribute>
<jsp:attribute name="mytag:d4">6</jsp:attribute>

Dynamic Attributes Syntax

For a tag to declare that it accepts dynamic attributes, it must implement the DynamicAttributes interface. The syntax is as follows:

public interface DynamicAttributes

You must also configure an entry for the tag in the TLD to indicate dynamic attributes are accepted. For any attribute that is not declared in the TLD for this tag, instead of getting an error at translation time, the setDynamicAttribute() method is called, with the name and value of the attribute. You configure the tag to remember the names and values of the dynamic attributes.

The setDynamicAttribute() method is called when a tag declared to accept dynamic attributes passes an attribute that is not declared in the TLD. The syntax is as follows:

public void setDynamicAttribute(java.lang.String uri, java.lang.String localName, java.lang.Object value)

The parameter values are as follows:

A JspException is thrown if the tag handler signals that it does not accept the given attribute. The container must not call doStartTag() or doTag() for this tag.

For more information on these interfaces, refer to the DynamicAttributes API at


Skip navigation bar  Back to Top Previous Next