Home > Articles > Open Source > Ajax & JavaScript

JSP Custom Tags

  • Print
  • + Share This
If you're a software developer with some Java experience and you want to build Web applications based on XML, XSLT, Java servlets, Java applets, and JavaServer Pages, New Riders' author Westy Rockwell helps get you started by reviewing JSP custom tags and giving you tips for expanding your knowledge of this topic. He shows you some of the widely available and professional software tools that cover all this technology and introduces you to many of the tasks you will encounter in your own Web application projects.
This article is excerpted from XML, XSLT, Java, and JSP: A Case Study in Developing a Web Application, by Westy Rockwell.

This article gives you a brief overview of JavaServer Pages (JSP) custom tags. It is not a comprehensive JSP custom tag reference, but is meant to support your other resources, deepen your understanding of some aspects of tag libraries, and promote your own experimental approach to JSP technology.

It should certainly not be your only resource for this very rich subject. As you learn about JSP tag libraries, be sure to check the wealth of resources available to you at the main JSP Web site (http://java.sun.com/product/jsp/). You should definitely consult the excellent documentation available from the creators of JSP at http://java.sun.com/products/jsp/docs.html.

What Are JSP Custom Tags?

To see where JSP tag libraries (taglibs) and custom tags fit in, it helps to take a lightning tour of JavaServer Pages. In JSP 1.1, a page is made up of elements and template data. An element is something whose meaning is understood and that the JSP container responds to. Everything that is not an element is template data, such as static HTML content for a browser to display. An element belongs to one of three types:

  • a directive element

  • a scripting element

  • an action

A directive element directs the JSP container in a global manner, such as by controlling aspects of page translation or by providing a URL to locate a needed resource. These elements use syntax based on <%, as follows:

<%@ directive ...%>

Scripting elements make it possible to use scripting languages on the page (in JSP 1.1, only Java). A scripting element can be a declaration, a scriptlet, or an expression. Scripting elements also use syntax based on <%:

<%! declaration %>
<% scriptlet %>
<%= expression %>

A declaration element creates something that is available to all other scripting elements (such as an instance variable). A scriptlet enables you to put any code into the compiled page, allowing its logic to control and affect other page content. An expression is a complete Java expression that can be evaluated at response time, usually providing a string to be included in the JSP output stream.

An action encapsulates useful functionality. Standard actions are always available in JSP, while custom actions are added to JSP by means of the tag extension mechanism provided. Actions are expressed using an XML-based syntax, as follows:

<x:foo attr1="..." attr2="..." attr3="..." />

Actions can have a body and be expressed as follows:

<x:foo attr1="..." attr2="..." attr3="..." >

JSP custom tags are used for adding actions to the built-in ones available in JSP. The JSP 1.1 Specification, Section 2.11, has this to say about actions:

Actions may affect the current out stream and use, modify and/or create objects. Actions may, and often will, depend on the details of the specific request object received by the JSP page. The JSP specification includes some action types that are standard and must be implemented by all conforming JSP containers. New action types are introduced using the taglib directive. The syntax for action elements is based on XML; the only transformation needed is due to quoting conventions (see Section 7.5).

Elsewhere, the JSP Specification also says the following:

Actions permit the encapsulation of useful functionality in a convenient form that can also be manipulated by tools.

JSP custom tags can be added to the built-in JSP tags to extend JSP in a portable manner. Each custom tag packages Java code into a reusable "action" element, which can easily be added to JSP documents. A group of one or more custom tags is made available to JSP documents as a tag library. This component technology extends JSP, furthering its aims of portability, reusability, separation of static and dynamic Web content, and a wide choice of development tools.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.