Final Exam and Project: The When, the What, the How Exactly

Here's your Final Exam:

  1. Implement the Lindley Portfolio Problem using Java Servlets

  2. Implement the Calculator Problem using Java Server Pages

Only two problems.

Solve them and post the working versions and the source code under protected.

Post the answers on your web sites by Fri Dec 14, 7pm.

Please include comments in your code or annotate the code as we do in lecture notes.

Group Project Information

If you have a project please come and turn it in in person.

  1. When you turn in the project you should also turn in the final.

  2. I will ask you a couple of questions about both.

  3. You will need to schedule an appointment here.
If you're working on the default project

  1. Implement Lab Fourteen and have it indexed on your site.

  2. Turn in (in person) a written report (described below).

The written report for the default project should explain two things:
  1. Clearly outline the flow of information from the moment someone enters a new book in FooBar's web interface to the moment that book is retrieved and shown by the servlet in Tomcat.

  2. Provide a completely honest annotation of the indenting parser. Do your best to describe your understanding of it. Use any means to make it more explicit: draw a picture, write a poem, flow chart it, whatever.
Turn the written report with your final next week.

Same procedure about making an appointment.

Please note:

  1. If you have a project you don't need to do the default project.

  2. If you don't have a project you need to turn in the default project.

That's where grading ends.

For completeness I will provide the remaining of the shopping cart and all of the notes for the Java networked games on the web site next week. Use them if you need them but I am afraid they will be posted too late for you to use them to build anything that would count towards your grade this semester. But you could use them regardless.

For grading purposes it's either your project or Lab Fourteen now.

Today in class we will go over JSP and also present a brief XML tutorial.

Tutorial first. XML is a data description language. Here's an example:

<?xml version="1.0" standalone="yes"?>
<document>
  <customer>
    <name>
      <last_name>Bird</last_name>
      <first_name>Larry</first_name>
    </name>
    <date>October 15, 2001</date>
    <orders>
      <item>
        <product>Tomatoes</product>
        <number>8</number>
        <price>$1.25</price>
      </item>
      <item>
        <product>Oranges</product>
        <number>24</number>
        <price>$4.98</price>
      </item>
    </orders>
  </customer>
  <customer>
    <name>
      <last_name>Jordan</last_name>
      <first_name>Michael</first_name>
    </name>
    <date>October 20, 2001</date>
    <orders>
      <item>
        <product>Bread</product>
        <number>12</number>
        <price>$14.95</price>
      </item>
      <item>
        <product>Apples</product>
        <number>6</number>
        <price>$1.50</price>
      </item>
    </orders>
  </customer>
  <customer>
    <name>
      <last_name>Kukoc</last_name>
      <first_name>Tony</first_name>
    </name>
    <date>October 25, 2001</date>
    <orders>
      <item>
        <product>Asparagus</product>
        <number>12</number>
        <price>$2.95</price>
      </item>
      <item>
        <product>Lettuce</product>
        <number>6</number>
        <price>$11.50</price>
      </item>
    </orders>
  </customer>
</document>
Let's see a picture of this:

What's in black represents a syntactic category.

What's in red is a terminal, a literal value.

In blue we have unexpanded categories (lack of space).

So this is a partial picture of this.

How do we access this structure?

I call this file customer.xml

I place it in /u/dgerman/practice.

I also create a file One.java.

Let's go through it step by step.

burrowww.cs.indiana.edu% cat One.java
import org.w3c.dom.*;
import org.apache.xerces.parsers.DOMParser;

public class One {
    public static void main(String[] args) {

    }
}
burrowww.cs.indiana.edu% javac One.java
burrowww.cs.indiana.edu%
We first import the classes that we need. These classes support the W3C DOM interfaces such as Node and Element. We also need the class that implements the DOM parser.

burrowww.cs.indiana.edu% cat One.java
import org.w3c.dom.*;
import org.apache.xerces.parsers.DOMParser;

public class One {
    public static void main(String[] args) {

        DOMParser parser = new DOMParser(); 

    }
}
burrowww.cs.indiana.edu% javac One.java
burrowww.cs.indiana.edu% 
To parse an XML document we need a DOMParser object, a parser, which I will call parser.

DOMParser is derived from XMLParser, which in turn is based on the Object class.

burrowww.cs.indiana.edu% cat One.java
import org.w3c.dom.*;
import org.apache.xerces.parsers.DOMParser;

public class One {
    public static void main(String[] args) {

        DOMParser parser = new DOMParser(); 

        try {

            parser.parse(args[0]); 

        } catch (Exception e) {
            e.printStackTrace(System.err); 
        }

    }
}
burrowww.cs.indiana.edu% javac One.java
burrowww.cs.indiana.edu% java One customer.xml
burrowww.cs.indiana.edu% 
To actually parse an XML document you use the parse method of the parser object.

Check the documentation for the parse method now. It is in XMLParser and overloaded. We use the one that starts from a String (called a System ID). I will let the user specify the name of the document to parse on the command line by parsing args[0].

Note that you don't need to pass the name of a local file to the parse method - you can pass the URL of a document on the Internet, and the parse method wil retrive that document.

That document could be a file or the output of a script, it doesn't matter.

Since parse throws exceptions we try to run it and catch the exceptions if any.

burrowww.cs.indiana.edu% cat One.java
import org.w3c.dom.*;
import org.apache.xerces.parsers.DOMParser;

public class One {
    public static void main(String[] args) {

        DOMParser parser = new DOMParser(); 

        try {

            parser.parse(args[0]); 
            Document doc = parser.getDocument(); 


        } catch (Exception e) {
            e.printStackTrace(System.err); 
        }

    }
}
burrowww.cs.indiana.edu% javac One.java
burrowww.cs.indiana.edu% java One customer.xml
burrowww.cs.indiana.edu%
If the document is successfully parsed you can get a Document object based on the W3C DOM, corresponding to the parsed document, using the parser's getDocument method.

The Document interface is described on-line. Note that parse returns an object that belongs to a class that implements Document.

The Document interface is based on the Node interface which supports the W3C Node object.

A Node represents a single node in the document tree.

The Node interface has many methods that you can use to work with nodes.

One can use

to get information about the node.

One can also use

to move in the document tree relative to a specific node.

A Node object also has data members that hold constant values corresponding to the various node types in the W3C DOM.

The types of nodes we'll look at are mentioned in the indenting parser of last time.

burrowww.cs.indiana.edu% cat One.java
import org.w3c.dom.*;
import org.apache.xerces.parsers.DOMParser;

public class One {
    public static void main(String[] args) {

        DOMParser parser = new DOMParser(); 

        try {

            parser.parse(args[0]); 
            Document doc = parser.getDocument(); 

            NodeList nodeList = doc.getElementsByTagName(args[1]); 


        } catch (Exception e) {
            e.printStackTrace(System.err); 
        }

    }
}
burrowww.cs.indiana.edu% javac One.java
burrowww.cs.indiana.edu% java One customer.xml CUSTOMER
burrowww.cs.indiana.edu% 
At this point we have access to the root node of the document.

Our goal is to check how many CUSTOMER elements the document has so we use the

getElementsByTagName (from Document)
method to get a
NodeList
object containing a list of all CUSTOMER elements.

burrowww.cs.indiana.edu% cat One.java
import org.w3c.dom.*;
import org.apache.xerces.parsers.DOMParser;

public class One {
    public static void main(String[] args) {

        DOMParser parser = new DOMParser(); 

        try {

            parser.parse(args[0]); 
            Document doc = parser.getDocument(); 

            NodeList nodeList = doc.getElementsByTagName(args[1]); 

            int len = nodeList.getLength(); 

            System.out.println(args[0] + " has " + len + " " + 
                               args[1] + " elements."); 


        } catch (Exception e) {
            e.printStackTrace(System.err); 
        }

    }
}
burrowww.cs.indiana.edu% javac One.java
burrowww.cs.indiana.edu% java One customer.xml CUSTOMER
customer.xml has 0 CUSTOMER elements.
burrowww.cs.indiana.edu% java One customer.xml customer
customer.xml has 3 customer elements.
burrowww.cs.indiana.edu% 
Note that the process is case-sensitive.

NodeList also has an API, and we use

getLength (returning an int)
from its API to find out how many CUSTOMER elements we have in the document.

Let's now review the type of nodes we could have.

At the beginning of the document is the XML declaration, and the type of this node matches the constant Node.DOCUMENT_NODE defined in the Node interface. To get the document element of the document we're parsing (the root element) we call the getDocumentElement method. To handle nodes of type Node.ELEMENT_NODE we simply report the element's name.

If an element node has attributes we can run the getAttributes() method to obtain a NodeList that holds all the attributes, stored as Attr objects.

Because the Attr interface is built on the Node interface you can can use either the getNodeName and getNodeValue methods to get to the attribute's name and value.

To get to the child elements of an element node you call getChildNodes.

Handling text nodes we can use getNodeValue().

We also need to take care of closing tags for element nodes.

import org.w3c.dom.*;
import org.apache.xerces.parsers.DOMParser;

public class One {
    public static void main(String[] args) {

	DOMParser parser = new DOMParser(); 

	try {

	    parser.parse("customer.xml"); 
	    Document doc = parser.getDocument(); 

            Node one = doc.getDocumentElement(); 

            System.out.println(one.getNodeName()); 

            NodeList nodeList = one.getChildNodes(); 

            Node two = nodeList.item(1); 

	    System.out.println(two.getNodeName()); 


	} catch (Exception e) {
	    e.printStackTrace(System.err); 
	}

    }
}

Last updated: Dec 6, 2001 by Adrian German for A348/A548