Web Programming Week 2 Old Dominion University Martin Klein <>

advertisement
Web Programming
Week 2
Old Dominion University
Department of Computer Science
CS 418/518 Fall 2010
Martin Klein <mklein@cs.odu.edu>
09/07/10
Defining the Web / HTTP
• HTTP was originally defined by Request for
Comments (RFCs)1945, 2068, 2616
– and several others for defining URLs, URIs, etc.
• While RFC 2616 remains canonical for HTTP, we
have a new document for the “Web” (URIs +
protocols + formats), we have a slightly revisionist
but ultimately useful unifying document:
– The Architecture of the World Wide Web, Volume
One.
• http://www.w3.org/TR/webarch/
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL
NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and
"OPTIONAL" in this document are to be interpreted as described in
RFC 2119.
1. MUST
This word, or the terms "REQUIRED" or "SHALL", mean that the
definition is an absolute requirement of the specification.
2. MUST NOT
This phrase, or the phrase "SHALL NOT", mean that the
definition is an absolute prohibition of the specification.
How To Read
RFCs
3. SHOULD
This word, or the adjective "RECOMMENDED", mean that there
may exist valid reasons in particular circumstances to ignore a
particular item, but the full implications must be understood and
carefully weighed before choosing a different course.
4. SHOULD NOT
This phrase, or the phrase "NOT RECOMMENDED" mean that
there may exist valid reasons in particular circumstances when the
particular behavior is acceptable or even useful, but the full
implications should be understood and the case carefully weighed
before implementing any behavior described with this label.
5. MAY
This word, or the adjective "OPTIONAL", mean that an item is
truly optional. One vendor may choose to include the item because a
particular marketplace requires it or because the vendor feels that
it enhances the product while another vendor may omit the same item.
An implementation which does not include a particular option MUST be
prepared to interoperate with another implementation which does
include the option, though perhaps with reduced functionality. In the
same vein an implementation which does include a particular option
MUST be prepared to interoperate with another implementation which
does not include the option (except, of course, for the feature the
option provides.)
(quoting from RFC 2119)
Examples from RFC 2616
10.4.6
405 Method Not Allowed
The method specified in the Request-Line is not allowed for the
resource identified by the Request-URI. The response MUST include
an Allow header containing a list of valid methods for the requested
resource.
10.4.4
403 Forbidden
The server understood the request, but is refusing to fulfill it.
Authorization will not help and the request SHOULD NOT be
repeated.
10.4.9
408 Request Timeout
The client did not produce a request within the time that the server was
prepared to wait. The client MAY repeat the request without
modifications at any later time.
Reading the W3C Arch
• Principles
– fundamental “rule” or “law” that is widely applicable
• Constraints
– definitions imposed by the architects of the Web
– “MUST”, “SHALL”
• Good Practice
– things you should be doing, but they can’t / shouldn’t
be defined as constraints
– “SHOULD”, “RECOMMENDED”
Principle: Global Identifiers
• Principle: Global Identifiers
– Global naming leads to global network effects.
Good practice: Identify with
URIs
• Good practice: Identify with URIs
– To benefit from and increase the value of the
World Wide Web, agents should provide URIs
as identifiers for resources.
Constraint: URIs Identify a
Single Resource
• Constraint: URIs Identify a Single Resource
– Assign distinct URIs to distinct resources.
Uniform Resource Identifiers
URI RFC 3986 (2396)
URL
RFC 1738
URN
RFC 2141
URI Schemes
foo://username:password@example.com:8042/over/there/index.dtb;type=animal?name=ferret#nose
\ /
\________________/\_________/ \__/
\___/ \_/ \_________/ \_________/ \__/
|
|
|
|
|
|
|
|
|
|
userinfo
hostname port
|
|
parameter query fragment
|
\_______________________________/ \_____________|____|____________/
scheme
|
| | |
|
authority
|path|
|
|
|
|
path
interpretable as filename
|
___________|____________
|
/ \ /
\
|
urn:example:animal:ferret:nose
interpretable as extension
taken from: http://en.wikipedia.org/wiki/URI_scheme
Important Web Architecture
Concepts (As defined by the Web Architecture)
remember:
• URIs identify Resources
• Representations represent
Resources
• When URIs are dereferenced,
they return representations
(i.e., a resource is never returned)
taken from: http://www.w3.org/TR/webarch/
W3C Web Architecture
Representation 2
URI
Represents
Identifies
Resource
The tools we have to solve the
interoperability problem are:
• Resource
• URI
• Representation
Content Negotiation
Represents
Representation 1
slide from Herbert Van de Sompel
LAMP
• Chapters 3, 10 of textbook
– more info: http://dev.mysql.com/doc/
• Quick review of relational databases
– normalization
– referential integrity
• Basic MySQL commands
“Graphic Novel” Super Heroes
(from chapter 10)
name
real
name
John
Smith
power 1
power 2
power 3
Strength
X-ray
vision
flight
Soap Stud
Efram
Jones
Speed
The
Dustmite
Dustin
Huff
Strength
Clean
Freak
Dirtiness
Laser
Vision
lair
address
123
Poplar
Ave
123
Poplar
Ave
452 Elm
St. #3D
city
state
zip
Townsburg
OH
45293
Townsburg
OH
45293
Burgtown
OH
45201
What if we need to add a super hero with more than 3 powers?
1st Normal Form
• Eliminate repeating columns
• Add primary key to table
– Unique
– Must not change
• Maintain “atomicity“
– Each cell is atomic, has only one item of data
1NF
id
1
1
1
2
3
3
3
name
real
name
John
Smith
power
•
•
•
lair
address
Clean
Strength 123
Freak
Poplar
Ave
Clean
John
X-ray
123
Freak
Smith vision
Poplar
Ave
Clean
John
fligh t
123
Freak
Smith
Poplar
Ave
Soap
Efram Speed
123
Stud
Jones
Poplar
Ave
The
Dustin Strength 452
Dustmit e Huff
Elm St.
#3D
The
Dustin Dirt iness 452
Dustmit e Huff
Elm St.
#3D
The
Dustin Laser
452
Dustmit e Huff
Vision
Elm St.
#3D
add primary key to tables
eliminate repeating columns
each attribute is atomic
city
state
zip
Townsbu rg OH
45293
Townsbu rg OH
45293
Townsbu rg OH
45293
Townsbu rg OH
45293
Burgtown
OH
45201
Burgtown
OH
45201
Burgtown
OH
45201
What if John Smith changes his name?
2nd Normal Form
• Honor 1st Normal Form
• Create separate tables for data duplicated
across rows
• Be aware of relationships!
– 1:1
– 1:m
– m:n
•
•
2NF
id
lair_id
1
1
2
1
3
2
id
1
2
3
4
5
6
power
Strength
X-Ray
vision
Flight
Speed
Dirt iness
Laser
Vision
name
real
name
Clean
John
Freak
Smith
Soap
Efram
Stud
Jones
The
Dustin
Dustmit e Huff
align
id
good
1
good
2
satisfy 1NF
create separate tables for data
duplicated across rows
lair
address
123 Poplar
Ave
452 Elm
St. #3D
city
state
zip
Townsbu rg OH
45293
Burgtown
45201
OH
evil
char_ id
1
1
1
2
3
3
3
power_ id
1
2
3
4
1
5
6
Are “city“ and “state“ directly related to the lairs?
3rd Normal Form
• Honor 1st & 2nd Normal Form
• Create separate tables for any transitive or
partial dependencies
•
•
3NF
id
lair_id
1
1
2
1
3
2
id
1
2
power
Strength
X-Ray
vision
Flight
Speed
Dirt iness
Laser
Vision
3
4
5
6
name
real
name
Clean
John
Freak
Smith
Soap
Efram
Stud
Jones
The
Dustin
Dustmit e Huff
char_ id
1
1
1
2
3
3
3
satisfy 2NF
create separate tables for any
transitive or partial dependencies
align
id
zip_id
good
1
45293
good
2
45201
lair
address
123 Poplar
Ave
452 Elm
St. #3D
evil
power_ id
1
2
3
4
1
5
6
id
45293
45201
city
state
Townsbu rg OH
Burgtown
OH
see note on p. 283 on why good/evil is not in a separate table
That’s About as Far As We’ll Go
• Other normal forms are possible (BCNF,
4NF, 5NF)
– take a database class if you’re interested
• Referential integrity
– a foreign key (“link”) into another table is no
longer valid
– “404 Errors” are bad in databases and should
not happen
• how bad is a function of the data itself…
MySQL Hierarchy
server=mln-web.cs.odu.edu
database=tennisplayers
table 1
table 2
table 3
database=superheroes
table 1
table 2
table 3
Manipulating Tables & Databases
•
•
•
•
•
CREATE - create new databases, tables
ALTER - modify existing tables
DELETE - erase data from tables
DESCRIBE - show structure of tables
INSERT INTO tablename VALUES - put data in
table
• UPDATE - modify data in tables
• DROP - destroys table or database (values +
structure)
more: http://dev.mysql.com/doc/refman/5.0/en/sql-syntax.html
Native MySQL Data Types
• Unlike Perl, PHP and other civilized
languages, MySQL is big into data types:
– http://dev.mysql.com/doc/refman/5.0/en/datatypes.html
– many examples in chapter 3, 10
SQL Query Form
SELECT [fieldnames]
FROM [tablenames]
WHERE [criteria]
ORDER BY [fieldname to sort on] [DESC]
LIMIT [offset, maxrows]
more: http://dev.mysql.com/doc/refman/5.0/en/select.html
look at chapters 3 and 10 for code examples
Download