This tip focuses on how you can build and use custom JSP tag libraries in IBM's WebSphere Development Studio Client for iSeries (v5.1) (WDSC). Web application servers such as IBM's WebSphere and Apache Tomcat provide runtime environments for Java servlets and JSPs that use JSP tag libraries.Basic Java Web application model
Sun Microsystems' JSR 52 defines the Java Servlet version 2.3 and Java Server Page version 1.2 specification defining Java Web applications. The specification is part of Sun's J2EE specification (Java 2 Enterprise Edition) defining enterprise server application support. The specifications are important because they provide open standards that all Java Web application servers implement (WebSphere, Tomcat, WebLogic, JBoss, etc.) making Java Web applications portable across many server platforms.
Custom JSP tags
Custom tags are user-defined JSP tags that add well-defined sets of application behavior in a clean, structured manner to JSPs much like custom CL commands add rich, flexible program behavior to CL programs. When a JSP is translated to a servlet by the JSP compiler, the tag is replaced by a set of Java operations on the tag handler object. This is similar conceptually to how macros work.
Custom JSP tags were first defined in Sun's JSP specification 1.1. Older versions of Web application servers, such as WebSphere 3.02, don't support custom tag libraries. The current JSP specification level is 1.2. The Jakarta Tomcat server version 4.x from Apache supports JSP 1.2. WebSphere version 3.5x and WebSphere 4.x support JSP 1.1.
Like the format of HTML tags, custom JSP tags have elements and attributes that customize a tag's behavior. They can be combined in many ways, including nesting (something CL commands can't do). They also have full access to all the objects in the servlet context.
JSP tags are packaged in tag libraries as a jar file. A Tag Library Descriptor (TLD) file defines all the JSP tags in a given library, along with the parameters each tag supports and where the tag library file is located. The TLD file format has a standard header layout that includes information on the tag library: version, JSP version, short name, URI to uniquely identify the tag library in the application context and a description. In addition, the file will contain one or more tag definitions and listener definitions.
A Web application using the tag library usually stores the tag library in the WEB-INF directory along with the TLD file. Any JSPs using the tag library declare a tag library directive referencing the tag library. Web developers then can use specific JSP tags from the library in the JSP wherever needed.
Custom JSP tag type processing
The tag handler class is responsible for interacting with server side objects in the application (e.g., your existing shopping cart bean) and the JSP page. Tags optionally have attributes that are passed in as name value pairs (name="Jim") to customize the behavior of the tag handler.
Some tags have "body content" elements that are items declared as part of the page within the tag start and end. This is referred to as the body content of a tag. When processing a tag with body content, the body will usually be either evaluated (for an iteration or condition) or updated (for output on the page).
The release method releases any allocated resources in the tag handler object. The doStart and doEnd tags are invoked when a custom tag is encountered in the page. Return values tell the Web container how to process the JSP page.
- Return options for doStart: Include the current body content, but don't process it; process the current body content or do not evaluate the body of the JSP page
- Return options for doEnd: Evaluate the rest of the JSP page, skip the rest of the JSP page
for simple tag handlers that are invoked but don't directly manipulate the JSP body content
(e.g., prints out "Hello World", a formatted date string etc which is added to the JSP page
for more complex handlers that update the JSP body content of the page directly
(e.g., creating new body content on a JSP page such as an order line item table)
Subclasses of BodyTagSupport class can manipulate the body content of the JSP page using the following:
doInitBody - before the body of the tag is evaluated
doAfterBody - after the body of the tag is evaluated
Different tags can share and access the same objects that are stored in the application context. For example, one tag may create a new order and another may ship the order on two JSPs. A tag handler can access the JSP through the PageContext retrieved from the application, request or response objects.
Tags execute in sequence during page processing by the servlet engine. Attributes are passed in. The doStart, doEnd methods are called. If the tag is a subclass of BodyTagSupport, the doInitBody and doAfterBody methods can also be called to provide output to the page content directly.
A Listener is a class that is instantiated and registered with the container to handle specific servlet life cycle events. When an event occurs, the container invokes the registered method to handle the event.
Web development cost, time, risks reduced
WebSphere originally offered built-in proprietary tags for database access. Today the open standard for custom tag libraries has replaced proprietary tags in most Web application servers. Libraries of JSP tags are available from many sources now, including both billable libraries such as IBM, BEA and others and non-billable libraries such as Apache, SourceForge and other organizations. (See references below for more.)
In addition, Java developers can create custom tag libraries for their own business logic, making it easily accessible to Web developers in a reusable form. Packaging Java beans as custom tag libraries in Web applications offers many advantages:
- It promotes reuse of Java beans for customizable business logic, common functions.
- The same beans can be reused in multiple user interfaces: Web applications, PDAs, Java applications.
- Web developers don't have to learn Java to build dynamic Web applications for data and transactions.
- WDSC Page Designer allows developers to visually use custom JSP tags in JSPs without coding low-level JSP and HTML tags.
Having built JSP applications under JSP 1.0 without custom tag libraries, these tag libraries greatly reduce the cost, time and risks in developing powerful Web applications.
Apache Jakarta Taglibs Project
The Taglibs project is providing a wide variety of standard, open-source JSP tag libraries, including Bean Scripting Framework (BSF) for integrating scripting languages and Java beans, DB Tags integrating database access dynamically, XSL tags for transforming XML documents, IO tags for HTTP, FTP, XRPC and SOAP requests and more.
Jakarta Struts Project
The Struts tag library provides a framework for building internationalized Web applications that implement the Model-View-Controller design pattern. Struts includes a comprehensive set of utility custom tags for handling the following:
- HTML forms
- JavaBeans components
- Logic processing
You can also create your own custom tag libraries as needed to encapsulate reusable business logic in Web applications, for example.
Steps for building and using custom tags in WDSC
- Create a tag library of one or more Tag Handler Classes
- Create a Tag Library Descriptor file (.tld)
- Package the tag library
- Copy the tag library to an application's WEB-INF folder
- Copy the Tag Library Descriptor to the WEB-INF folder
- Add a page directive declaring the Tag library in a JSP
- Add JSP tags as needed to the JSP
- Testing the Application with the custom JSP tag
If you are using a tag library created by someone else (the Jakarta Taglibs or Struts projects), begin with Step 4.
Using custom tags in WDSC
You work primarily in the Web perspective building a Web application when you use custom JSP tag libraries. Normally I work in the Java perspective when I'm building a tag library. WDSC's dynamic project configuration makes it easy to configure and use tag libraries and other resources during development and testing.
For more detailed information on each specific step, refer to the online help in WDSC. The steps below are a simple example I did to build and use a very limited custom tag. The example tag handler class (DatePrint) prints only a date string within a JSP.
After creating a new Web project in WDSC, I generated the original JSP application I used the tag for using the WDSC Web database application wizard to search, select and view data from DB2. With a limited investment in learning WDSC first, you should be able to generate your own Web database application with the wizard and then follow the process below to create a custom tag and add it to a JSP you created.