What are custom tags useful for ?

Stacy Curl

Lots of things :)

One of the interesting uses for custom tags is empowering web site designers who don't know Java (poor things). A simple custom tag can be used to change 'complex' code like:

Current Date is: <%=new Date()%>
to
Current Date is: <mytag:currentdate/>

Even though the reduction in complexity is small (some might say none) the use of a tag encapsulates the code, so more sophisticated stuff can be implemented later without changes in the JSP. Internationalised dates, graphical animating dates, etc.

More complex code can include iteration over collections:

<mytag:getusers id="users"/>
<mytag:iterate overwhat="users" id="currentuser">
  <mytag:displayuser id="currentuser"/>
</mytag:iterate>
database access:
<mytag:dbgetusers db="UserDB" username="jsp" password="jsp" id="users"/>
<mytag:iterate overwhat="users" id="currentuser">
  <mytag:displayuser id="currentuser"/>
</mytag:iterate>

(With some coding effort the tags can be made similar)
One can say that just about any Java code can be put into a custom tag although this soon hits a brick wall as custom tags require more coding than simple 'script like' Java.

See: Which is better, Custom tags or JavaBeans ?

There are some applications of custom tags that have no alternative in JSP. One such application is post-processing of JSP generated output. Say we had an page that needed to contain a word count, this couldn't be implemented in JSP because once some text has been put into the JSPWriter you can't access it anymore. It is possible when using custom tags in the following way:
<mytag:getwordcount placeholder="$WordCount$">
    Blah blah blah ($WordCount$ Words)
</mytag:getwordcount>
then in the code for getwordcount:
public class WordCountTag extends BodyTagSupport
{
  private String m_placeholder;

  public int doStartTag() throws JspTagException
  {
    return EVAL_BODY_TAG;
  }

  public int doAfterBody() throws JspTagException
  {
    try {
      if (bodyContent != null) {
        String text = bodyContent.getString();
        int wordCount = calculateWordCount(text);
        String newText = replacePlaceholder(text, wordCount);
        bodyContent.getEnclosingWriter().print(newText);
      }
    } catch (IOException ie) {}

    return EVAL_PAGE;
  }

  public int doEndTag() throws JspTagException
  {
    return EVAL_PAGE;
  }

  private int calculateWordCount(String text)
  {
    //...
  }

  private String replacePlaceholder(String input,
      int wordCount)
  {
    //...
  }
}
The great thing about using custom tags for post processing is that it requires minimal or no changes to the JSP, if you were implementing last minute changes to Wahoo! to ensure that the text 'Cost = 0$' never appears it would be trivial to do so:
<mytags:nofreebies>
  rest of web page
</mytags:nofreebies>
Or even better:
<mytags:postprocessing processingSequence="someExternalResource">
  rest of web page
</mytags:postprocessing>

...

someExternalResource:
com.wahoo.jsp.processing.NoSwearing
com.wahoo.jsp.processing.NoFreebies

A bad thing about custom tags is that they don't seem to have an equal footing to standard jsp tags, so you can't have a <jsp:include ... > tag within a custom tag (custom tags cannot call out.flush, and jsp:include requires flush="true"), but hopefully this will change with JSP 1.2
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact