Professional Java Server Programming J2EE Edition
Chapter 12
Attributes can be either required or optional. This is specified in the TLD, as is whether attributes can take
the value of JSP expressions at runtime, as we mentioned earlier.
Let's suppose we decide to pass a name as an attribute, and change our simple example to display the following:
First, we need to write a tag handler with a name property. With this minor change, it's pretty much like
HelloTag:
package tagext;
import java.io.IOException;
import java.util.Date;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
// Hello tag accepting a name attribute.
public class AttribHelloTag extends TagSupport
{
private String name;
// Property getter for name
public String getName() {
return name;
}
// Property setter for name
public void setName(String name) {
this.name = name;
}
public int doEndTag() throws JspTagException {
String dateString = new Date().toString();
try {
pageContext.getOut().write("Hello <b>" + name + "</b>.<br/>");
pageContext.getOut().write("My name is " + getClass().getName() +
" and it's " + dateString + "<p/>");
}
catch (IOException ex) {
throw new JspTagException("Hello tag could not write to JSP out");
}
return EVAL_PAGE;
}
}
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.