CSci 4608 Principles of Web Programming, UMM: XSD validation in a servlet.

Source files

The path to the xml and xsd files is relative to jakarta-tomcat-5.0.30/bin. At this point the files are right in the directory. In general, this is not a good idea, creating a subfolder would be better.

Compiling and running the servlet

Since schema validation uses Xerces, the path to xercesImpl.jar must be added to the classpath for compilation. Xerces is included as a part of the standard Tomcat distribution, with the path

Therefore the compilation path command for the servlet and the error-handling class is:

javac -classpath .:jakarta-tomcat-5.0.30/common/endorsed/xercesImpl.jar:jakarta-tomcat-5.0.30/common/lib/servlet-api.jar
(assuming that the servlet's .java files are in the directory that contains jakarta-tomcat-5.0.30 directory, otherwise you need to adjust the paths accordingly)

"Running" the servlet just means copying all of its files into the class directory of your application (we are using the servlets-examples as our application). Don't forget to add the servlet (but not the supporting classes!) to the web.xml file. Since Xerces is included as a part of a standard distribution, its .jar file is accessible by Tomcat. Use Tomcat manager to reload the application (see the problem set for instructions).

The code

A few changes had to be made to the validation program to make it into a servlet. The most important change is that all necessary output must be written to the PrintWriter of the HttpRequest. It is up to you to decide which messages should be printed on the page, but all of them should be added to the PrintWriter. Note that the error class ShowValidationErrorsServlets needs to get access to the PrintWriter, so the PrintWriter is passed as a parameter to its constructor. Also note that doGet() cannot throw a SAXException (it overwrites an inherited doGet() method that only throws IOException and ServletException), therefore all SAXExceptions must be caught in the method. Same goes for doPost().

import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.SAXParser;

public class CheckSchemaServlet extends HttpServlet {

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException

        PrintWriter out = response.getWriter();

        // Set the filenames here:
        String xmlSource = "shiporder.xml";
        String xsdSchema = "shiporder.xsd";

        out.println("<body bgcolor=\"white\">");

        out.println("<title> Schema validation </title>");

        // strings to set up properties
        String W3C_XML_SCHEMA =
        String JAXP_SCHEMA_SOURCE =

        // the parser variable
        XMLReader parser = null;

        // create the XML parser
        try {
            // get a new parser from the factory
            parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");

            // XML Schema is used as the schema language
            parser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
            // Set up the schema file

            // both lines are needed to make the program
            // validate against the schema.
            // Otherwise it just validates well-formedness
            parser.setFeature("", true);

            // default contents handler
            parser.setContentHandler(new DefaultHandler());
            // set customized error handler 
	    // (needed to display validation errors)
            parser.setErrorHandler(new ShowValidationErrorsServlets(out));

        } catch  (SAXException e) {
            // print out the error message
            out.println("Cannot create a parser");
            // rethrow the exception
            //throw e;

        try {
            FileInputStream theStream = new FileInputStream(xmlSource);
            if (parser != null) {
                parser.parse(new InputSource(theStream));
        } catch (IOException e) {
            out.println("Cannot open a file");
            //throw e;
        } catch (SAXException e) {
            out.println("The document is invalid");
            //throw e;



import org.xml.sax.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ShowValidationErrorsServlets implements ErrorHandler {
    private PrintWriter out;

    public ShowValidationErrorsServlets(PrintWriter out) {
        this.out = out;

    public void warning(SAXParseException exception) {
        // Ignore warnings
        out.println("WARNING: " + exception.getMessage());

    public void error(SAXParseException exception) {
        out.println("VALIDATION ERROR: " + exception.getMessage());
        // throw exception;


    public void fatalError(SAXParseException exception)
        throws SAXException {
        out.println("WELL-FORMEDNESS ERROR: " + exception.getMessage());
        // A well-formedness error
        throw exception;



CSci 2101 home page

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.