Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
316
Chapter 14 Creating Custom JSP Tag Libraries
Listing 14.4 SimplePrimeTag.java
package coreservlets.tags;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
import java.math.*;
import coreservlets.*;
/** Generates a prime of approximately 50 digits.
* (50 is actually the length of the random number
* generated the first prime above that number will
* be returned.)
*/
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);
}
}
The Tag Library Descriptor File
The general format of a descriptor file is almost always the same: it should
contain an XML version identifier followed by a
DOCTYPE
declaration
followed by a
taglib
container element. To get started, just download a
sample from
http://www.coreservlets.com/
. The important part to
understand is what goes in the
taglib
element: the
tag
element. For tags
without attributes, the
tag
element should contain four elements between
and
:
1.
name
, whose body defines the base tag name to which the prefix
of the
taglib
directive will be attached. In this case, I use
simplePrime
to assign a base tag name of
simplePrime
.
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved