Defining a Simple Tag Handler
Class: Example
package cwp.tags;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
import java.math.*;
import cwp.*;
public class SimplePrimeTag
extends TagSupport
{
protected int len = 50;
public int
doStartTag
() {
try {
JspWriter out = pageContext.getOut();
BigInteger prime =
Primes.nextPrime(Primes.random(len));
out.print(prime);
} catch(IOException ioe) {
System.out.println("Error generating prime: " + ioe);
}
return(SKIP_BODY);
}}
81
JSP
www.corewebprogramming.com
Defining a Simple Tag Library
Descriptor
Start with XML header and DOCTYPE
Top level element is
taglib
Each tag defined by tag element containing:
name, whose body defines the base tag name.
In this case, I use
simplePrime
tagclass, which gives the fully qualified class name of the
tag handler. In this case, I use
cwp.tags.SimplePrimeTag
bodycontent, which gives hints to development
environments. Optional.
info, which gives a short description. Here, I use
Outputs a random 50 digit prime.
82
JSP
www.corewebprogramming.com
41