46-929 Web Technologies

48
46-929 Web Techn ologies 1 46-929 Web Technologies Lecture 1 Introduction

description

46-929 Web Technologies. Lecture 1 Introduction. Course Web Site. http://www.andrew.cmu.edu/~mm6. Course TA’s. Yunning Wang [email protected]. Structure of the Course. Lectures / class participation Homeworks (programming) Final exam. Readings. - PowerPoint PPT Presentation

Transcript of 46-929 Web Technologies

Page 1: 46-929 Web Technologies

46-929 Web Technologies

1

46-929 Web Technologies

Lecture 1 Introduction

Page 3: 46-929 Web Technologies

46-929 Web Technologies

3

Course TA’s

Yunning [email protected]

Page 4: 46-929 Web Technologies

46-929 Web Technologies

4

Structure of the Course

•Lectures / class participation

•Homeworks (programming)

•Final exam

Page 5: 46-929 Web Technologies

46-929 Web Technologies

5

Readings

The required text is “XML and Java” Second edition Addison-Wesley

Readings will be assigned each week.

Readings from the web will also be assigned.

Page 6: 46-929 Web Technologies

46-929 Web Technologies

6

Grading

• Homework (2-3) 50%

• Final Exam 50%

Page 7: 46-929 Web Technologies

46-929 Web Technologies

7

Selected Course Topics

• Web Applications, XML and Java• Parsing XML Documents• Generating and Serializing XML Documents• Working with DOM• Working with SAX• Xpath and XSLT

Page 8: 46-929 Web Technologies

46-929 Web Technologies

8

Selected Course Topics

• Servlets and Java Server Pages• XML, JDBC and Databases• The Financial Product Markup Language • XML Messaging using JAXM and SOAP• Web Services (WSDL, UDDI)• XML Digital Signature, XML Encryption• Data Binding using JAXB

Page 9: 46-929 Web Technologies

46-929 Web Technologies

9

Introduction

XML and Java

Chapter 1 Web Applications, XML, and Java

Page 10: 46-929 Web Technologies

46-929 Web Technologies

10

IntroductionIn this course we will be concerned with B2B applications.

We will study how applications can send, receive,verify and manipulate XML documents.

Definition: By Web Application we mean a distributed application based on Web technologies: HTTP, HTML, and the family of XML technologies.

Page 11: 46-929 Web Technologies

46-929 Web Technologies

11

IntroductionDefinition: By traditional three-tier application we mean applications consisting of:

First tier – Browsers that act as a human interface Second tier – a program running in a web server Third tier – backend systems that provide databases and transaction services

Let’s replace the browser with a program…

Page 12: 46-929 Web Technologies

46-929 Web Technologies

12

From web for eyeballs to web for programs

Consider a PowerWarning application allows usersTo register their geographical position and their temperature concerns.

Users will receive e-mail when the temperatureexceeds the user specified parameters.

Page 13: 46-929 Web Technologies

46-929 Web Technologies

13

[1] <html>[2] <head>[3] <title>Weather Report</title>[4] </head>[5] <body>[6] <h2>Weather Report -- White Plains, NY </h2>[7] <table border=1>[8] <tr><td>Date/Time</td><td align=center>11 AM EDT Sat Jul 25

1998</td></tr>[9] <tr><td>Current Tem.</td><td align=center>70&#176;</td></tr>[10] <tr><td>Today’s High</td><td align=center>82&#176;</td></tr>[11] <tr><td>Today’s Low</td><td align=center>62&#176;</td><tr>[12] </table>[13] </body>[14] </html>

Suppose that we know that the weather information is availablefrom the web at http://www.xweather.com/White_Plains_NY_US.html.

Page 14: 46-929 Web Technologies

46-929 Web Technologies

14

•Strategy 1:

For the current temperature of White Plains, go to line 9,column 46 of the page and continue until reaching the nextampersand.

•Strategy 2:

For the current temperature of the White Plains, go to thefirst <table> tag, then go to the second <tr> tag within thetable, and then go to the second <tg> tag within the row.

Neither of these seems very appealing…

Page 15: 46-929 Web Technologies

46-929 Web Technologies

15

<?xml version=“1.0”?>

<!DOCTYPE WeatherReport SYSTEM

“http>//www.xweather.com/WeatherReport.dtd”>

<WeatherReport>

<City>White Plains</City>

<State>NY</State>

<Date>Sat Jul 25 1998</Date>

<Time>11 AM EDT</Time>

<CurrTemp unit=“Farenheit”>70</CurrTemp>

<High unit=“Farenheit”>82</High>

<Low unit=“Farenheit”>62</Low>

</Weather Report>

XML would help

Page 16: 46-929 Web Technologies

46-929 Web Technologies

16

•Strategy 3:

For the current temperature of White Plains, N.Y., go to the <CurrTemp> tag.

Page 17: 46-929 Web Technologies

46-929 Web Technologies

17

XML

Mobile users

PC usersHttp://www.xweather.com

WeatherReportapplication

WML

HTML

PowerWarningapplication

Applicationprograms

XML

Email notifications

RegistrationsXML

XSLT

Page 18: 46-929 Web Technologies

46-929 Web Technologies

18

XML

• Extensible Markup Language (really a meta- language)• Generic syntax (not like HTML) • Simple, human-readable tags• From Web for eyeballs to Web for programs• Tools exist that allows us to easily process any type of XML document

Page 19: 46-929 Web Technologies

46-929 Web Technologies

19

Introduction

XML and Java Chapter 2

Basics of Parsing Documents

Page 20: 46-929 Web Technologies

46-929 Web Technologies

20

Department.xml<?xml version="1.0" encoding="utf-8"?><department> <employee id="J.D"> <name>John Doe</name> <email>[email protected]</email> </employee>

<employee id="B.S"> <name>Bob Smith</name> <email>[email protected]</email> </employee>

Page 21: 46-929 Web Technologies

46-929 Web Technologies

21

<employee id="A.M"> <name>Alice Miller</name> <url href="http://www.foo.com/~amiller/"/> </employee></department>

Page 22: 46-929 Web Technologies

46-929 Web Technologies

22

SimpleParse.java// Download Xerces from http://xml.apache.org/xerces2-j/index.html// Two Jar files are needed- xercesImpl.jar and xmlParserAPIs.jar// add these to the classpath

/** * From XML and Java Chapter 2 SimpleParse.java **/

import org.w3c.dom.Document;import org.apache.xerces.parsers.DOMParser;import org.xml.sax.SAXException;import java.io.IOException;

Page 23: 46-929 Web Technologies

46-929 Web Technologies

23

public class SimpleParse { public static void main(String[] argv) { if (argv.length != 1) { System.err.println( "Usage: java SimpleParse <filename>"); System.exit(1); } try {

// Creates a parser object DOMParser parser = new DOMParser(); // Parses an XML Document parser.parse(argv[0]); // Gets a Document object Document doc = parser.getDocument();

Page 24: 46-929 Web Technologies

46-929 Web Technologies

24

} catch (SAXException se) { System.out.println("Parser error found: " +se.getMessage()); System.exit(1); } catch (IOException ioe) {

System.out.println("IO error found: " + ioe.getMessage());

System.exit(1); } }}

D:\McCarthy\www\95-733\examples\chap02>java SimpleParse department.xml

D:\McCarthy\www\95-733\examples\chap02>

Page 25: 46-929 Web Technologies

46-929 Web Technologies

25

Let’s Introduce an Error

<?xml version="1.0" encoding="utf-8"?><department> <employee id="J.D"> <name>John Doe</name> <email>[email protected]</email> </employee>

<employee id="B.S"> <name>Bob Smith</name> <email>[email protected]</email> </employee>

This code is OK.

Page 26: 46-929 Web Technologies

46-929 Web Technologies

26

<employee id="A.M"> <name>Alice Miller <url href="http://www.foo.com/~amiller/"/> </employee></department>

Missing closing tag on name.

The document is not well-formed.

Page 27: 46-929 Web Technologies

46-929 Web Technologies

27

D:\McCarthy\www\95-733\examples\chap02>java SimpleParse department.xml[Fatal Error] department.xml:16:5: The element type "name" must be terminated by the matching end-tag "</name>".Parser error found: The element type "name" must be terminated by the matching end-tag "</name>".

After displaying the error status anexception was throw to the application

Page 28: 46-929 Web Technologies

46-929 Web Technologies

28

Simple Validation

Page 29: 46-929 Web Technologies

46-929 Web Technologies

29

department-dtd.xml<?xml version="1.0" encoding="utf-8"?><!DOCTYPE department SYSTEM "department.dtd"><department> <employee id="J.D"> <name>John Doe</name> <email>[email protected]</email> </employee>

<employee id="B.S"> <name>Bob Smith</name> <email>[email protected]</email> </employee>

Page 30: 46-929 Web Technologies

46-929 Web Technologies

30

<employee id="A.M"> <name>Alice Miller</name> <url href="http://www.foo.com/~amiller/"/> </employee></department>

Page 31: 46-929 Web Technologies

46-929 Web Technologies

31

department.dtd

<!ELEMENT department (employee)*><!ELEMENT employee (name, (email | url))><!ATTLIST employee id CDATA #REQUIRED><!ELEMENT name (#PCDATA)><!ELEMENT email (#PCDATA)><!ELEMENT url EMPTY><!ATTLIST url href CDATA #REQUIRED>

Page 32: 46-929 Web Technologies

46-929 Web Technologies

32

SimpleParseWithValidation.java

/** * SimpleParseWithValidation.java **/import org.w3c.dom.Document;import org.xml.sax.InputSource;import org.xml.sax.SAXException;import org.xml.sax.SAXParseException;import org.xml.sax.ErrorHandler;import org.apache.xerces.parsers.DOMParser;import java.io.IOException;

Page 33: 46-929 Web Technologies

46-929 Web Technologies

33

public class SimpleParseWithValidation {

public static void main(String[] argv) { if (argv.length != 1) { System.err.println("Usage: java "+ "SimpleParseWigthValidation <filename>"); System.exit(1); } try { // Creates parser object DOMParser parser = new DOMParser(); // Sets an ErrorHandler parser.setErrorHandler(new MyErrorHandler());

Page 34: 46-929 Web Technologies

46-929 Web Technologies

34

// Tells the parser to validate documents parser.setFeature( "http://xml.org/sax/features/validation", true); // Parses an XML Document parser.parse(argv[0]); // Gets a Document object Document doc = parser.getDocument(); // Does something } catch (Exception e) { e.printStackTrace(); } }}

Page 35: 46-929 Web Technologies

46-929 Web Technologies

35

MyHandler.java

import org.xml.sax.ErrorHandler;import org.xml.sax.SAXException;import org.xml.sax.SAXParseException;

public class MyErrorHandler implements ErrorHandler {

/** Constructor. */ public MyErrorHandler(){ }

Page 36: 46-929 Web Technologies

46-929 Web Technologies

36

/** Warning. */ public void warning(SAXParseException ex) { System.err.println("[Warning] "+ getLocationString(ex)+": "+ ex.getMessage()); } /** Error. */ public void error(SAXParseException ex) { System.err.println("[Error] "+ getLocationString(ex)+": "+ ex.getMessage()); } /** Fatal error. */ public void fatalError(SAXParseException ex) { System.err.println("[Fatal Error] "+ getLocationString(ex)+": "+ ex.getMessage()); }

Page 37: 46-929 Web Technologies

46-929 Web Technologies

37

/** Returns a string of the location. */ private String getLocationString(SAXParseException ex) { StringBuffer str = new StringBuffer();

String systemId = ex.getSystemId(); if (systemId != null) { int index = systemId.lastIndexOf('/'); if (index != -1) systemId = systemId.substring(index + 1); str.append(systemId); } str.append(':'); str.append(ex.getLineNumber()); str.append(':'); str.append(ex.getColumnNumber()); return str.toString(); }}

Get the filename after the /’s.

Page 38: 46-929 Web Technologies

46-929 Web Technologies

38

Runs without error

D:\McCarthy\www\95-733\examples\chap02>java SimpleParseWithValidation department-dtd.xml

D:\McCarthy\www\95-733\examples\chap02>

Page 39: 46-929 Web Technologies

46-929 Web Technologies

39

Using the same dtd…

<!ELEMENT department (employee)*><!ELEMENT employee (name, (email | url))><!ATTLIST employee id CDATA #REQUIRED><!ELEMENT name (#PCDATA)><!ELEMENT email (#PCDATA)><!ELEMENT url EMPTY><!ATTLIST url href CDATA #REQUIRED>

Page 40: 46-929 Web Technologies

46-929 Web Technologies

40

Introduce an error<?xml version="1.0" encoding="utf-8"?><!DOCTYPE department SYSTEM "department.dtd"><department> <employee> <name>John Doe</name> <email>[email protected]</email> </employee>

<employee id="B.S"> <name>Bob Smith</name> <email>[email protected]</email> </employee>

Page 41: 46-929 Web Technologies

46-929 Web Technologies

41

<employee id="A.M"> <name>Alice Miller</name> <url href="http://www.foo.com/~amiller/"/> </employee></department>

D:\McCarthy\www\95-733\examples\chap02>java SimpleParseWithValidation department-dtd2.xml[Error] department-dtd2.xml:4:13: Attribute "id" is required and must be specified for element type "employee".

The parser does not throw an exception.

Page 42: 46-929 Web Technologies

46-929 Web Technologies

42

Make a fatal error (not well-formed)

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE department SYSTEM "department.dtd"><department> <employee id="J.D."> <name>John Doe</name> <email>[email protected]</email> </employee>

<employee id="B.S"> <name>Bob Smith</name> <someTag> id="A.M"> <email>[email protected]</email> </employee></department>

Page 43: 46-929 Web Technologies

46-929 Web Technologies

43

D:\McCarthy\www\95-733\examples\chap02>java SimpleParseWithValidation department-dtd2.xml

[Error] department-dtd2.xml:10:39: Element type "someTag" must be declared.[Fatal Error] department-dtd2.xml:12:5: The element type "someTag" must be terminated by the matching end-tag "</someTag>".org.xml.sax.SAXParseException: The element type "someTag" must be terminated bythe matching end-tag "</someTag>". at org.apache.xerces.parsers.DOMParser.parse(Unknown Source) at SimpleParseWithValidation.main(SimpleParseWithValidation.java:31)

An exception is thrown to the callerfor this Fatal Error.

Page 44: 46-929 Web Technologies

46-929 Web Technologies

44

JAXP

• An API but really an abstraction layer• Does not provide a new means of parsing XML• Does not add to DOM, SAX or XSLT• Does not provide new functionality in handling XML

• JAXP enables applications to parse and transform XML documents independent of a particular XML processing implementation. Just change a system variable or classpath setting and you change parsers.

• Six classes included in JDK1.4 in javax.xml.parsers package

Page 45: 46-929 Web Technologies

46-929 Web Technologies

45

SimpleParseJAXP.java/** * SimpleParseJAXP.java **/import java.io.IOException;import org.w3c.dom.Document;import org.xml.sax.ErrorHandler;import org.xml.sax.SAXException;import org.xml.sax.SAXParseException;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.FactoryConfigurationError;import javax.xml.parsers.ParserConfigurationException;

Note: no mentionof xerces.

Page 46: 46-929 Web Technologies

46-929 Web Technologies

46

public class SimpleParseJAXP {

public static void main(String[] argv) { if (argv.length != 1) { System.err.println( "Usage: java SimpleParseJAXP <filename>"); System.exit(1); } try { // Creates document builder factory DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // Tells the parser to validate documents factory.setValidating(true);

Page 47: 46-929 Web Technologies

46-929 Web Technologies

47

// Tells the parser to be aware namespaces factory.setNamespaceAware(true); // Creates builder object DocumentBuilder builder = factory.newDocumentBuilder(); // Sets an ErrorHandler builder.setErrorHandler(new MyErrorHandler()); // Parses the document Document doc = builder.parse(argv[0]); } catch (ParserConfigurationException pe) { pe.printStackTrace(); } catch (SAXException se) { se.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } }}

Page 48: 46-929 Web Technologies

46-929 Web Technologies

48

Suppose we want Xerces to be our parser

D:\McCarthy\www\95-733\examples\chap02>java -Djavax.xml.parsers.DocumentBuilderFactory= org.apache.xerces.jaxp.DocumentBuilderFactoryImpl SimpleParseJAXP department-dtd.xml