Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
320
Chapter 14 Creating Custom JSP Tag Libraries
One of the most common things to do in the attribute handler is to simply
store the attribute in a field that will later be used by 
doStartTag
 or a similar
method. For example, following is a section of a tag implementation that adds
support for the 
message
 attribute.
private String message = "Default Message";
public void setMessage(String message) {
this.message = message;
}
If the tag handler will be accessed from other classes, it is a good idea to
provide a 
getAttributeName
 method in addition to the 
setAttributeName
method. Only 
setAttributeName
 is required, however.
Listing 14.7 shows a subclass of 
SimplePrimeTag
 that adds support for
the 
length
 attribute. When such an attribute is supplied, it results in a call to
setLength
, which converts the input 
String
 to an 
int
 and stores it in the
len
 field already used by the 
doStartTag
 method in the parent class. 
Listing 14.7 PrimeTag.java 
package coreservlets.tags;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
import java.math.*;
import coreservlets.*;
/** Generates an N digit random prime (default N = 50).
 *  Extends SimplePrimeTag, adding a length attribute
 *  to set the size of the prime. The doStartTag
 *  method of the parent class uses the len field
 *  to determine the approximate length of the prime.
 */
public class PrimeTag extends SimplePrimeTag {
  public void setLength(String length) {
    try {
      len = Integer.parseInt(length);
    } catch(NumberFormatException nfe) {
      len = 50;
    }
  }
}
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




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

jsp web hosting

 

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