Design and develop custom tags

Custom tag libraries

A custom tag library is a set of custom tags that invoke custom actions in a JavaServer Pages (JSP) file. Tag libraries reduce the task of embedding excessive amounts of Java code in JSP pages by moving the functionality provided by the tags into tag implementation classes.

Tag libraries are usually created by developers who are proficient in the Java programming language and can be used by Web designers who may not know Java, but would like to enhance their Web site by taking advantage of Java encoded tag libraries.

WebSphere Studio includes a library of custom tag libraries as part of the Sun Microsystems JSP 1.2 Specification making it easy for you to select and insert a particular tag from a tag library.

Tag libraries can be developed in WebSphere Studio. You can optionally add some of the Jakarta Project tag libraries directly from the Dynamic Web Project wizard. Several of the common tag libraries are available as Web project features.

NOTE: If you do not include these features when you create a Web project, you can add them from the project's Web Project Features properties page later.

Tag libraries:

The web.xml file provides the link between the directive used in the application and the actual JAR file containing the classes that execute the function.

Creating a custom tag library

At some point, you may wish to create your own custom tag library. This example shows you the steps for creating a new tag library called copyrightInfo, which outputs the copyright information at the bottom of web page.

In order to use custom JSP tags, you need to define three separate components: the tag handler class that defines the tag's behavior, the tag library descriptor file that maps the XML element names to the tag implementations, and the JSP file that uses the tag library.

  1. Creating a tag handler class

    From the Java perspective:

    1. Select titan-web application.

    2. Select File > New > Class. Type the following in the New Java Class window:

      Package : com.titan.tags

      Name : CopyrightInfo

      Superclass : javax.servlet.jsp.tagext.TagSupport

      Creating a tag handler class

    3. Click Finish. The source file will open automatically.

    4. Delete the automatic code and replace it with the following code:

      package com.titan.tags;
      import javax.servlet.jsp.JspException;
      import javax.servlet.jsp.JspWriter;
      import javax.servlet.jsp.tagext.TagSupport;
       * @author Mikalai Zaikin
      public class CopyrightInfo extends TagSupport {
      	public int doStartTag() throws JspException {
      		try {
      			JspWriter out = pageContext.getOut();
      			out.print("<p>(c) Copyright Mikalai Zaikin 2005</p>");
      		} catch (IOException ioe) {
      			System.out.println("Error in CopyrightInfo: " + ioe);
      		return (SKIP_BODY);
      	public int doEndTag() {
      		return EVAL_PAGE;

    5. Save and close the file.

  2. Creating a tag library descriptor file

    The tag must be defined in a TLD file. To create the TLD file from the Web perspective:

    1. Select titan-web application.

    2. Select the Web module WEB-INF.

    3. Right-click the WEB-INF folder.

    4. Select New > Other > Simple > File. Click Next.

    5. Name the parent folder titan-web/WebContent/WEB-INF/tlds.

      Name the file titanTags.tld

      Creating a tag library descriptor file

    6. Click Finish. The source file will open automatically. Delete the automatic code and replace it with the following code:

      <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" 
      	<description>Tag library for Titan application</description>

      Here is a defintion for the taglib element for JSP 1.2:

      <!ELEMENT taglib (tlib-version, jsp-version, short-name, uri?,
      		display-name?, small-icon?, large-icon?, description?,
      		validator?, listener*, tag+) >

    7. Save and close the file.

  3. Using a custom tag library

    Before the CopyrightInfo tag can be used, the following taglib reference must be made:

    1. Place the following taglib definition on the first line of the JSP source file:

      <%@ taglib uri="/WEB-INF/tlds/titanTags.tld" prefix="tt" %>

    2. Place copyright information tag in the code.

      <tt:copyrightInfo />

    3. Save and close the file.

Professional hosting     Belorussian informational portal         Free SCBCD 1.3 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     Free SCJP 5.0 (Tiger) Study Guide     Free Mock Exam Engine     Free SCWCD 1.4 Study Guide