Top Banner
Tomcat Java and XML
38

Tomcat Java and XML. Announcements Final homework assigned Wednesday Two week deadline Will cover servlets + JAXP.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

TomcatJava and XML

Page 2: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

Announcements

Final homework assigned Wednesday Two week deadline Will cover servlets + JAXP

Page 3: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

Tomcat Configuration

1. Download the software. Go to http://jakarta.apache.org/builds/jakarta-tomcat-4.0/release/ and download and unpack the zip file for the latest version (4.1.12 as of last revision of this page).

2. Enable the ROOT context. Edit install_dir/conf/server.xml and uncomment this line: <Context path="" docBase="ROOT" debug="0"/>. Not necessary in Tomcat 4.0.3 and earlier.

Page 4: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

Tomcat Configuration

3. Enable the invoker servlet. Go to install_dir/conf/web.xml and uncomment the servlet-mapping element that maps the invoker servlet to /servlet/*. Not necessary prior to Tomcat 4.1.12.

4. Change the port to 80. Edit install_dir/conf/server.xml and change the port attribute of the Connector element from 8080 to 80.

Page 5: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

Tomcat Configuration

5. Turn on servlet reloading. Edit install_dir/conf/server.xml and add a DefaultContext subelement to the main Service element and supply true for the reloadable attribute.

6. Set the JAVA_HOME variable. Set it to refer to the base JDK directory, not the bin subdirectory.

Page 6: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

Servlet example

Goal: Create StudentSort program with web-enable interface?

Ideas?

Page 7: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

Outline

What is XML? JAXP

– SAX API– DOM API– XSLT API

JAX-RPC JAXM JAXR

Page 8: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

Java and XML

Page 9: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

XML Basics

Think of XML as a language-neutral text-based way of representing simple objects.

For example, in C:struct Auto{

char* make,

char* model,

int quantity,

double price

};

Page 10: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

XML Basics, cont

In Java:class Auto{

public String make;

public String model;

public int quantity;

public double price

}

Note that C and Java Auto objects are not human-readable, can not be interchanged between languages and platforms (in C case).

Page 11: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

XML version of Auto

XML is a formalism for describing simple structures in a language neutral way:

<!DOCTYPE Auto [

<! ELEMENT Auto (make,model,quantity,price)>

<!ELEMENT make (#PCDATA)>

<!ELEMENT model (#PCDATA)>

<!ELEMENT quantity (#PCDATA)>

<!ELEMENT price (#PCDATA)>

]>

Page 12: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

XML version of Auto

<!DOCTYPE Auto [ <! – Each XML file is stored in a document whose name is

the same as the root node -- > <! ELEMENT Auto (make,model,quantity,price)> <! – Auto has four attributes -- > <!ELEMENT make (#PCDATA)> <! – make is parsed character data -- > <!ELEMENT model (#PCDATA)> <!ELEMENT quantity (#PCDATA)> <!ELEMENT price (#PCDATA)>]>

Page 13: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

Making Auto objects

In Cstruct Auto a;a.make = “Ford”;a.model = “Pinto;etc. ...

In JavaAuto a = new Auto();a.make = “Ford”;etc. ...

Page 14: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

Making auto objects, cont.

XML is not a programming language! We make a car object in an xml file: <auto>

<make>Ford</make> <model>Pinto</model> <quantity>100</quantity> <price>1200.50</price> </auto>

Think of this as like a serialized java object.

Page 15: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

XML vs. DTD

Note that there are two parts to what we did– Defining the “structure” layout– Defining an “instance” of the structure

The first is done with a Document Type Descriptor (DTD)

The second is the XML part Both can go in the same file, or an XML

file can refer to an external DTD file We will look at the syntax of each

Page 16: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

Aspects of XML syntax

It is illegal to omit closing tags XML tags are case-sensitive XML elements must be properly nested XML elements must have a root element XML preserves whitespaces XML comments:

< -- This is a comment -- >

Page 17: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

DTD syntax

Can be declared either before XML or in separate file as:

<!DOCTYPE root-element SYSTEM "filename">

Note: DTD’s are not required, but they are strongly recommended for:– ensuring correctness of XML document– standardizing industry-wide layouts

Page 18: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

Building blocks of XML

From DTD point of view, XML files are composed of:– Elements– Tags– Attributes– Entities– PCDATA– DATA

Page 19: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

Elements Elements are the fields of the structure, so to

speak:– The elements of auto are make, model, price, quantity

Tags are the things that surround (mark-up) elements (</make> ...)

Attributes provide extra information about elements– <model color=“red”/>

Entities– characters that are parsed by XML parser. Things like

&lt, &gt, &amp, etc.

Page 20: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

DTD elements, cont. PCDATA

– Parsed character data.– Text found between the start tag and the end tag of an

XML element.– Text that will be parsed by a parser. Tags inside the

text will be treated as markup and entities will be expanded. 

CDATA– Character data.– Text that will NOT be parsed by a parser. Tags

inside the text will NOT be treated as markup and entities will not be expanded.

Page 21: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

Examples

Study the three simple course examples:– cars.xml– employees.xml– computers.xml

Run the validating parser listed on the course webpage to verify that the xml document is well-formed

See dtd and xml tutorials listed on course website for more cool stuff.

Page 22: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

What can we do with this?

So, we have an XML document. We think of this as a text-based, language

neutral serialized object. But what can we do with it? How does it relate to html? Why is it useful?

Page 23: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

Displaying XML Notice that XML documents have no display

information Opening with a browser will show the raw source

nicely colored Display information can be associated with an

XML file by using one of two technologies:– CSS (Cascading Style Sheets)

• Simple, quick

– XSL (Extensible Stylesheet Language)• made up of XSLT, XPATH, and XSL Formatting Objects• Far slicker, more general, and complicated

Page 24: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

Displaying XML

We will not study how to display XML in this course

Please see CSS and XSL tutorials listed on course website

Main point is that data is decoupled from display. Display can be plugged in separately, but data can

still be accessed. Compare computers.html and computers.xml What is the advantage of computers.xml?

Page 25: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

Java and XML

To this point the only special thing about XML is that data and presentation were decoupled, unlike HTML.

The next key feature is language support– Language-neutral standards exists for parsing

XML documents!– This means that XML documents are great

ways of sharing data between programs, sending messages, etc.

Page 26: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

Parsing XML in Java

Two distinct traditional models:– DOM (Document Object Model)– SAX(Simplified API for XML Parsing)

Both are language-neutral standards with bindings specified for Java

These are defined as part of JAXP (Java API for XML Processing)

JDK contains DOM and SAX parser implementations

Page 27: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

DOM

DOM represents and XML document in java memory as a tree.

DOM is both read-write Getting a DOM parser in Java:

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

DocumentBuilderFactory factory =

DocumentBuilderFactory.newInstance();

DocumentBuilder builder = factory.newDocumentBuilder();

Page 28: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

Parsing XML to DOM

Once an instance of the parser is obtained: org.w3c.dom.Document document = builder.parse(xmlFile);

Once a Document object is obtained: org.w3c.dom.Node = document.getDocumentElement();

gets the root Node of the document

Page 29: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

Manipulating Nodes

Once the root node is obtained, typical tree methods exist to manipulate other elements:

boolean node.hasChildNodes()

NodeList node.getChildNodes()

Node node.getNextSibling()

Node node.getParentNode()

String node.getValue();

String node.getName();

String node.getText();

void setNodeValue(String nodeValue);

Node insertBefore(Node new, Node ref);

Page 30: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

What are Nodes?

Ok, but what information is placed in Nodes? Seehttp://java.sun.com/j2se/1.4.1/docs/api

/org.w3c.dom.Node.html

Note that the DOM parser parses everthing – white space, tags, comments, etc.

There are several ways to skip over info that doesn’t interest you– use e.g. (if node.getType == Node.TEXT_NODE)

– use DocumentBuilderFactory methods to configure parser with comment parsing, etc. turned off.

Page 31: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

Node Example

A good practice exercise is to do a depth-first traversal of a Document printing only the element informtion with level-dependent indentation.

I recommend doing this by yourself and then looking at my solution on the web site.

See ScanTree.java

Page 32: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

SAX parser

SAX parser scans an xml stream on the fly and responds to certain parsing events as it encounters them.

This is very different than digesting an entire XML document into memory.

Much faster, requires less memory. However, can not use to change XML. Need to reparse if you need to revisit data.

Page 33: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

Obtaining a SAX parser

Important classes javax.xml.parsers.SAXParserFactory;

javax.xml.parsers.SAXParser;

javax.xml.parsers.ParserConfigurationException;

//get the parser

SAXParserFactory factory = SAXParserFactory.newInstance();

SAXParser saxParser = factory.newSAXParser();

//parse the document

saxParser.parse( new File(argv[0]), handler);

Page 34: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

DefaultHandler

Note that an event handler has to be passed to the SAX parser.

This must implement the interface

org.xml.sax.ContentHanlder; Easier to extend the adapter

org.xml.sax.helpers.DefaultHandler

Page 35: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

Overriding Handler methods

Most typical methods to override

void startDocument()

void endDocument()

void startElement(...)

void endElement(...)

void characters(...) See examples on course website

Page 36: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

Writing XML from DOM

import javax.xml.transform.Source;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerFactory;import javax.xml.transform.Result;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;import org.w3c.dom.Document;import java.io.File;

public class WriteXML{ public static void write(Document doc, String filename) throws Exception { /* Prepare the DOM document for writing */ Source source = new DOMSource(doc); Result result = new StreamResult(new File(filename))); /* Write the DOM document to the file */ Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.transform(source, result);}}

Page 37: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

Using JAXP to simplify file input

Goal: read xml-based file into lottery servlet program.

Page 38: Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.

Using JAXP for messaging

Goal: build a socket program that exchanges xml-based messages