Professional Java Server Programming J2EE Edition Chapter 12
// Populate the data store. In a real application, this data would
// be sourced from a database or another part of the application.
public NameTag() {
infoHash.put("Rod", new PersonalInfo("Australian", "London"));
infoHash.put("Isabelle", new PersonalInfo("French", "Gabon"));
infoHash.put("Bob", new PersonalInfo("Australian", "Sydney"));
}
public int doStartTag() throws JspTagException {
String nationality = "Unknown";
String city = "Unknown";
// Test whether this tag has an ancestor of the required type,
// which we can use to obtain a name to lookup.
// Note that using the findAncestorWithClass static method is more
// flexible than using getParent(). getParent() will fail if
// one or more tags separate this tag from the desired tag in
// the runtime hierarchy of tag handlers.
NameContext nameContextAncestor = (NameContext)
TagSupport.findAncestorWithClass(this, NameContext.class);
// The exception thrown here will be handled by
//the JSP engine as normal.
// This will normally mean redirection to an error page.
if (nameContextAncestor == null) {
throw new JspTagException
("NameTag must only be used within a NameContext tag");
}
// If we get here, we have a valid ancestor from which
// we can obtain a context.
String name = nameContextAncestor.getName();
PersonalInfo pi = (PersonalInfo) infoHash.get(name);
if (pi != null) {
nationality = pi.getNationality();
city = pi.getCity();
}
pageContext.setAttribute("nationality", nationality);
pageContext.setAttribute("city", city);
return EVAL_BODY_INCLUDE;
}
// Inner class containing additional data retrieved for each name
private class PersonalInfo {
private String nationality;
private String city;
public PersonalInfo(String nationality, String city) {
this.nationality = nationality;
this.city = city;
}
public String getNationality() {
return nationality;
}
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.
|