- What Are JSP Custom Tags?
- How Do JSP Custom Tags Work?
- About This Article
How Do JSP Custom Tags Work?
This section gives you a brief overview of the mechanics of JSP custom tag extensions, as a basis for delving further into a study of JSP custom tags.
The Tag Handler Class
The behavior of a tag is determined by a JavaBean known as a Tag Handler class. It must implement either the javax.servlet.jsp.tagext.Tag interface or its BodyTag extension. Two classes in the servlet application programming interface (API) do that for you already: TagSupport and BodyTagSupport. Usually, you can extend one of them to define a Tag Handler class.
The Tag Library Descriptor
Tags are always part of a tag library, which is defined by an XML file called a tag library descriptor (TLD) file. Its main purpose is to connect the Tag Handler class with a tag name that will appear in the JSP document. It also gives the JSP container more information about the tags it describes. For example, it declares the tag attributes that can be used with the tag and tells whether they are required or optional.
The Name of a Tag
A JSP tag is approximately an XML tag. Some come standard with all JSP implementations. The name of a tag is in the form prefix:suffix. The prefix is defined in a taglib directive in the JSP file. The directive associates the tags with that prefix and with a particular tag library descriptor file. The suffix is the name that the TLD file associates with a Tag Handler class.
Tag Attributes and Tag Handler Properties
A tag can also have attributes, like an XML tag. (There are some differences in how quotes are used, however.) These attributes enable the JSP page author to pass values to the Tag Handler class. Each tag attribute corresponds to a property within the bean that implements the Tag (or BodyTag) interface.
The BodyContent Class and Body Content Processing
Body content can exist between a start tag and an end tag. The TLD description of the tag can enforce empty content or allow JSP or tag-dependent content. Tags that implement only the Tag interface, usually by extending TagSupport, can only ignore or include body content in the JSP. Tags that implement BodyTag, usually by extending BodyTagSupport, can manipulate and iteratively process body content. The BodyContent class is a special JspWriter object that encapsulates the body content while a tag is manipulating it.
Tag Action Methods in a Tag Handler
The methods within the Tag Handler class are related to the various parts of the tag. Thus, there is a method to handle the opening tag, called doStartTag(). Another method, doEndTag(), handles the closing tag. If a tag has attributes, then each one requires a property-setter method in the Tag Handler class and can have a get method. If the tag has a body, two other methods handles that: doInitBody() and doAfterBody(). Each method returns certain final static constants to control the sequential execution of these methods.
Context and Nesting of Tag Handler Instances
When a JSP is translated into source code for a servlet, the Tag Handler class for any custom tags on the JSP are instantiated within the _jspService() method of the servlet. The tag handler instance has a property set to refer to the powerful pageContext object of the JSP. Tags can nest. If a tag is nested in another, its parent property contains a reference to the tag it is nested in. Tags can find each other and share objects in any Web application scope. BodyContents can form a stack to facilitate nested manipulation of body content.
Translation-Time Tag Extension Methods
The javax.servlet.jsp.tagext package contains classes that implement JSP custom tags. Besides the ones mentioned previously, there are other classes that give the JSP container information at JSP translation time about tags and the variables they use:
TagAttributeInfo
TagData
TagExtraInfo
TagInfo
TagLibraryInfo
VariableInfo
The doStartTag() Method
Implementing the Tag interface implies defining a doStartTag() method. A Tag Handler class can either do that or extend the TagSupport class and override its doStartTag() method to begin the action. When the method begins, the JSP container will have set the pageContext property and also the parent property (null, if the tag is not nested). It will also have set all the tag attribute properties provided in the start tag. As the developer, you control whether the body content is processed next by returning the appropriate constant. Here is what a doStartTag looks like when you want to ignore body content:
public int doStartTag() throws JspException { // do something, or nothing return SKIP_BODY; }
This means that everything between the opening and closing tags of the custom JSP element will be ignored. If that is not the desired behavior, you can return a different value, as follows:
public int doStartTag() throws JspException { // do something, or nothing return EVAL_BODY_INCLUDE }
This means that everything between the opening and closing tags of the custom JSP element will be evaluated into the current output stream object. In the case of a simple, non-nested tag, that object starts out being the JspWriter instance named "out." The tag element in the TLD for the tag must not have a value of empty, of course. SKIP_BODY and EVAL_BODY_INCLUDE are the only return values for doStartTag() if only Tag is implemented. If BodyTag is implemented, a new BodyContent output stream will be created, and returning EVAL_BODY_TAG will throw a JspException. Tags that implement BodyTag should return either SKIP_BODY or EVAL_BODY_TAG, as follows:
public int doStartTag() throws JspException { // do something, or nothing return EVAL_BODY_TAG; }
After this last example of doStartTag(), the JSP container can invoke two other methods to process the body content: doInitBody() and doAfterBody(), discussed next.
The doInitBody() Method
The doInitBody() method can be used to do some processing before any body content is evaluated (into the BodyContent output stream). After the invocation of doInitBody(), the tag body content is evaluated and the doAfterBody() method is invoked.
The doAfterBody() Method
The doAfterBody() method is invoked after the first evaluation of the body content (if it is not empty). By returning SKIP_BODY, the doAfterBody() method can tell the JSP container that the processing of the body content is finished:
public int doAfterBody() throws JspException { // do something, or nothing return SKIP_BODY; }
Sometimes the tag handler class must continually process the body content of a custom tag in a loop. You can tell the JSP container to repeatedly evaluate the body content and doAfterBody() invocation by returning EVAL_BODY_TAG from doAfterBody(). Note that because processing can change the body content or its context, each body content evaluation can have differing results.
public int doAfterBody() throws JspException { //do something, or nothing return EVAL_BODY_TAG; }
The doEndTag() Method
Whether only the Tag interface or the BodyTag interface is implemented by a Tag Handler class, the doEndTag() method is invoked by the container. It can be used for any final processing in the action, whether body content has been evaluated or not. The container will call the release() method to release tag state.