Contents
JSP Tag Extensions
Tag extensions or custom tags are application-defined language extensions to JavaServer Pages. You can use custom tags in your JSP pages to do the following:
- Create static text (HTML, XML) or JSP output
- Create objects that are hidden from the JSP page or that can be seen as scripting variables in the JSP page
- Process a block of JSP page text
- Determine if a section of the calling JSP should be processed or skipped Custom-related tags are grouped together into a tag library.
This page talks about creating and using tag libraries.
Some custom-tag activities relate to the tag library, whereas others relate to individual tags within the library.
A tag is a bean that implements either the javax.servlet.jsp.tagext.
Tag or javax.servlet.jsp.tagext.BodyTag interfaces. The two classes, javax.servlet.jsp.tagext.TagSupport and java.servlet.jsp.tagext.BodyTagSupport, are convenience classes that the developer can subclass to create tags as well.
These classes implement the aforementioned interfaces respectively. You can read more about these two interfaces and classes later. A simple description of how custom tags work is that when the JSP engine comes across a tag in a JSP page, it invokes a method of the bean with which this tag is related. Throughout the rest of this chapter we will elaborate on this process.
Custom tags are referenced in your JSP page as an XML element, such as the examples shown below:
<mytaglib:atagwithbody>
This tag has a body
</mytaglib:atagwithbody >
Custom JSP tags may contain a body. The tag body can be static text or JSP code. The particulars of the tag's behavior govern how the JSP container interprets the tag body.
Custom tags may also be empty, that is without a body, as shown in the following example:
<mytaglib:anemptytag />
Custom tags may also contain attributes, as in the following:
<mytaglib:anemptytagwithattrs attr1="attr1Value" attr2="attr2Value" />
You can nest tags to any practical level, as shown in the following example:
<mytaglib:outertag >
This tag can contain plain old text
<%= session.getAttribute("userID") %>
<mytaglib:innertag anattr="attrvalue">
<p>This tag <I>too</I>
</mytaglib:innertag>
</mytaglib:outertag>
Custom tags follow XML syntax rules. You may be thinking that if a custom tag causes the invocation of a bean, why use custom tags at all? Why not code the bean and use the JSP action jsp:useBean instead?
Using Tags Versus Using JavaBeans
This action is limited to accessing the bean's exposed properties. Within your bean, your mutator methods influence the values of your bean properties. For example, the JSP code below accesses a bean that generates HTML of a certain font size and color. In addition, the bean has a property that, when true, reverses the text. The entire JSP page does not need to be shown; the relevant code illustrates the point.
<jsp:useBean id="exbean" class="chapter7.ExBean"/>
<jsp:setProperty name="exbean" property="fontSize" value="2" />
<jsp:setProperty name="exbean" property="fontColor" value="red" />
<jsp:setProperty name="exbean" property="reverse" value="false" />
<jsp:setProperty name="exbean" property="htmlline" value="This is <i>Line 1</i>" />
<p>HTML line <br>
<jsp:getProperty name="exbean" property="htmlline" />
<jsp:setProperty name="exbean" property="fontSize" value="5" />
<jsp:setProperty name="exbean" property="fontColor" value="blue" />
<jsp:setProperty name="exbean" property="reverse" value="true" />
<jsp:setProperty name="exbean" property="htmlline" value="Here's another line" />
<p>Another HTML line<br>
<jsp:getProperty name="exbean" property="htmlline" />
Here's the code within the bean class ExBean that sets the value of bean property htmlline. As with the preceding listing for the JSP page, the entire bean doesn't need to be shown because the set method for the htmlline property suffices.
public void setHtmlline( String line1 ) {
String locFontSize = getFontSize() ;
String color = getFontColor()
String linetext = (getReverse())?
((new StringBuffer(line1)).reverse()).toString() : line1 ;
htmlline = "<font size=" + locFontSize + ">" +
"<font color=" + color + ">" + linetext +
"</font></font>";
}
Compare how clumsy and inelegant the preceding example is with the following code:
<mytaglib:FormatLine fontSize="2"
fontColor="red"
reverse="false">
This is <i>Line 1</i>
</mytaglib:FormatLine>
<mytaglib:FormatLine fontSize="5"
fontColor="blue"
reverse="true">
Here's another line
</mytaglib:FormatLine>
Notice how natural the syntax of the custom tag fits with a page of HTML or XML text. By comparison, the JSP code for using the bean seems archaic. Getting, setting, and using bean property values in your JSP pages is a worthwhile and powerful feature. Sadly, using beans and the associated jsp:setProperty and jsp:getProperty actions clutters your pages with counterintuitive coding structures.