[ Team LiB ] Previous Section Next Section

"Hello World" Tag

To create a custom JSP tag, you must first create a Java class that acts as a tag handler. Whenever your custom tag appears in a JavaServer Page, the JSP engine invokes your tag handler. If your custom tag doesn't care about the body text between its opening and closing tags, you can use the simple TagSupport class, which implements the Tag interface. If you need to access and possibly change the body text within the opening and closing tags, you must subclass the BodyTagSupport class instead. The BodyTagSupport class implements the BodyTag interface, which allows you to access body text. Tags that implement these interfaces are known as classic tag handlers.

The IterationTag Interface

graphics/didyouknow_icon.gif

There's another interface you can use to build tags with. It's also a member of the "classic" family of interfaces. If your tag has a body that needs to be evaluated by the container, then you can implement the IterationTag interface. Your tag will not have access to the body but can still examine and affect variables and other contextual information. However, since the BodyTag interface extends the IterationTag interface, most developers use the BodyTag interface by extending the BodyTagSupport class. We won't discuss this interface any further.


For example, suppose you define a custom tag named <mytags:DoSomething> and you use it this way:


<mytags:DoSomething>
   Here is some text
</mytags:DoSomething>

If your tag handler implements only the Tag interface, it can't see the body text (that is, Here is some text). All it can do is decide whether the client can see the body text or not. However, your custom tag can generate its own output.

Listing 16.1 shows the HelloWorldTagHandler class that inserts the familiar "Hello World!" message into the JSP response. Because it doesn't need to access its body text, it subclasses TagSupport.

Listing 16.1 Source Code for HelloWorldTag.java
package examples.taglibs;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;

public class HelloWorldTag extends TagSupport
{
    public int doStartTag()
        throws JspException, IOException
    {
        JspWriter out = pageContext.getOut();
        out.println("<h1>Hello World!</h1>");
        return SKIP_BODY;
    }

public int doEndTag()
    {
        return EVAL_PAGE;
    }
}

The methods doStartTag and doEndTag are invoked by the JSP container at the points in the page where the custom tag starts and ends. Don't worry about the SKIP_BODY and EVAL_PAGE return values just yet. You'll see what they mean shortly.

Listing 16.2 shows a JSP that calls HelloWorldTag via a tag named <mytag:hello>. At this point, you don't know how to relate HelloWorldTag to <mytag:hello>. You will see that in the next section.

Listing 16.2 Source Code for TestHello.jsp
<%@ taglib uri="/hello" prefix="mytag" %>
<html>
<body>
<mytag:hello/>
</body>
</html>

Figure 16.1 shows the output from TestHello.jsp.

Figure 16.1. Custom tags can insert text into the response.

graphics/16fig01.gif

    [ Team LiB ] Previous Section Next Section