Java Reference
In-Depth Information
Example 19−1: ListServlets1.java (continued)
// After </servlet-class>, we've got the class name accumulated
servletClass = accumulator.toString().trim();
}
else if (name.equals("servlet")) {
// Assuming the document is valid, then when we parse </servlet>,
// we know we've got a servlet name and class name to print out
System.out.println("Servlet " + servletName +
((servletId != null)?" (id="+servletId+")":"") +
": " + servletClass);
}
}
/** This method is called when warnings occur */
public void warning(SAXParseException exception) {
System.err.println("WARNING: line " + exception.getLineNumber() + ": "+
exception.getMessage());
}
/** This method is called when errors occur */
public void error(SAXParseException exception) {
System.err.println("ERROR: line " + exception.getLineNumber() + ": " +
exception.getMessage());
}
/** This method is called when non-recoverable errors occur. */
public void fatalError(SAXParseException exception) throws SAXException {
System.err.println("FATAL: line " + exception.getLineNumber() + ": " +
exception.getMessage());
throw(exception);
}
}
Compiling and Running the Example
To run the previous example, you need the JAXP package from Sun. You can
download it by following the download links from http://java.sun.com/xml/ . Once
you've downloaded the package, uncompress the archive it is packaged in and
install it somewhere convenient on your system. In Version 1.0.1 of JAXP, the
download bundle contains two JAR files: jaxp.jar , the JAXP API classes, and
parser.jar , the SAX and DOM APIs and default parser implementations. To compile
and run this example, you need both JAR files in your classpath. If you have any
other XML parsers, such as the Xerces parser, in your classpath, remove them or
make sure that the JAXP files are listed first; otherwise you may run into version-
skew problems between the different parsers. Note that you probably don't want
to permanently alter your classpath, since you'll have to change it again for the
next example. One simple solution with Java 1.2 and later is to temporarily drop
copies of the JAXP JAR files into the jr e/lib/ext/ directory of your Java installation.
With the two JAXP JAR files temporarily in your classpath, you can compile and
run ListServlets1.java as usual. When you run it, specify the name of a web.xml file
on the command line. You can use the sample file included with the download-
able examples for this topic or specify one from your own servlet engine.
Search WWH ::




Custom Search