No Slide Title

Download Report

Transcript No Slide Title

Chapter 18
An introduction to the
Music Store web site
Java Servlets and JSPCH18
© 2003, Mike Murach & Associates, Inc.
Slide 1
The Home page
Java Servlets and JSPCH18
© 2003, Mike Murach & Associates, Inc.
Slide 2
The Catalog page
Java Servlets and JSPCH18
© 2003, Mike Murach & Associates, Inc.
Slide 3
The HTML for the Home page
<!doctype html public "-//W3C//DTD HTML 4.0
Transitional//EN">
<html>
<head>
<title>Fresh Corn Records</title>
<link rel="stylesheet" href="styles/murach.css">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<table cellpadding="5" border="0" width="756">
<!-- start the HTML for the header row -->
Java Servlets and JSPCH18
© 2003, Mike Murach & Associates, Inc.
Slide 4
The HTML (continued)
<tr>
<td colspan="3" height="79" bgcolor="#FFFFFF"
cellpadding="0">
<img src="images/storelogo.gif"
alt="Fresh Corn Records" width="606"
height="79">
</td>
</tr>
<tr>
<!-- end the HTML for the header row -->
<!-- start the HTML for the left column -->
<td width="160" valign="top" bgcolor="#FFFFCC">
<p>
<br><br>
Home<br><br>
Java Servlets and JSPCH18
© 2003, Mike Murach & Associates, Inc.
Slide 5
The HTML (continued)
<a href="catalog.htm">
Browse through our catalog</a><br><br>
<a href="cart">
Quick order an album</a><br><br>
<a href="email/join_email_list.jsp">
Join our email list</a><br><br>
<a href="customer_service.htm">
Contact customer service</a>
</p>
</td>
<!-- end the HTML for the left column -->
Java Servlets and JSPCH18
© 2003, Mike Murach & Associates, Inc.
Slide 6
The HTML (continued)
<!-- start the HTML for the middle column -->
<td width="404" valign="top">
<h1>Welcome to Fresh Corn Records!</h1>
<p>
Thanks for visiting. Make yourself at home. Feel free
to browse through our musical catalog. When you do,
you can listen to samples from the albums
on our site, or you can download selected sound files
and listen to them later. We think our catalog
contains some great music, and we hope you
like it as much as we do.
</p>
<p>
If you find an album that you like, we hope that
you'll use this site to order it. Most of the albums
we carry aren't available anywhere else!
</p>
</td>
<!-- end the HTML for the middle column -->
Java Servlets and JSPCH18
© 2003, Mike Murach & Associates, Inc.
Slide 7
The HTML (continued)
<!-- start the HTML for the right column -->
<td width="166" valign="top" bgcolor="#FFFFCC">
<h1>New release:</h1>
<img src="images/8601_cover_t.jpg"
width="80" height="80"><br>
<p>
<h4>86 (the band)<br>
True Life Songs and Pictures</h4>
<p>A refreshing mix of rock, country, and bluegrass
that will have you stomping your foot and crying
in your beer in no time.</p>
</p>
</td>
<!-- end the HTML for the left column -->
</tr>
Java Servlets and JSPCH18
© 2003, Mike Murach & Associates, Inc.
Slide 8
The HTML (continued)
<!-- start the HTML for the footer row -->
<tr>
<td colspan="3"><center>
<p class="copyright">&copy;
Copyright 2002 Mike Murach &amp; Associates, Inc.&nbsp;
All rights reserved.
</p></center>
</td>
</tr>
</table>
</body>
Java Servlets and JSPCH18
© 2003, Mike Murach & Associates, Inc.
Slide 9
The structure
index.htm
Browse through our catalog
Quick order an album
Join our email list
Contact customer service
customer_service.htm
cart/index.jsp
catalog.htm
email/join_email_list.jsp
Product links
Add to Cart for each product
Product links
product/index.htm
Add to Cart
Show Cart
Shopping Cart
application
Email application
Listen to Samples
Download application
Java Servlets and JSPCH18
© 2003, Mike Murach & Associates, Inc.
Slide 10
The directory structure
c:\tomcat
webapps
musicStore (the HTML file for the Home page)
admin (JSP and HTML files for the admin section)
albums (HTML files for all products)
8601
jr01
pf01
pf02
cart (JSP and HTML files for the cart section)
download (JSP files for the download section)
email (JSP and HTML files for the email section)
images (GIF and JPG image files)
sound (MP3 and RealAudio files for all products)
8601
jr01
pf01
pf02
styles (the cascading style sheet)
WEB-INF (the web.xml file for the application)
classes
music
com
javaexchange
dbConnectionBroker
admin (servlets for the admin section)
business (Java classes for business objects)
cart (servlets for the cart section)
data (Java classes for database access)
download (servlets for the download section)
email (servlets for the email section)
tags (tag handler and tei classes)
util (helper classes)
tlds (the tag library descriptor file)
Java Servlets and JSPCH18
© 2003, Mike Murach & Associates, Inc.
Slide 11
The directory structure of the web site
 The HTML documents and JSPs are stored in a directory that
identifies the name of the application.
 The servlet classes are stored in a directory that identifies the name
of the application.
 The business classes for all applications are stored in the
music.business package.
 The data access classes for all applications are stored in the
music.data package.
 The utility classes for all applications are stored in the music.util
package.
 The classes that define the custom JSP tags that can be used by all
applications are stored in the music.tags package (see chapter 9).
 The web.xml file is stored in the WEB-INF directory.
Java Servlets and JSPCH18
© 2003, Mike Murach & Associates, Inc.
Slide 12
The business objects
User
-firstName : String
-lastName : String
-emailAddress : String
-companyName : String
-address1 : String
-address2 : String
-city : String
-state : String
-zip : String
-country : String
-creditCardType : String
-creditCardNumber : String
-creditCardExpirationDate : String
+User()
+User(first : String,
last : String, email :
String, company :
String, address1 :
String, address2 : String,
city : String,
state : String,
zip : String,
country : String)
+setFirstName(f : String)
+getFirstName() : String
+setLastName(l : String)
+getLastName() : String
+setEmailAddress(e : String)
+getEmailAddress() : String
+setCompanyName(c : String)
+getCompanyName() : String
+setAddress1(a : String)
+getAddress1() : String
+setAddress2(a : String)
+getAddress2() : String
+setCity(c : String)
+getCity() : String
+setState(s : String)
+getState() : String
+setZip(z : String)
+getZip() : String
+setCountry(c : String)
+getCountry() : String
+setCreditCardType(c : String)
+getCreditCardType() : String
+setCreditCardNumber(n : String)
+getCreditCardNumber() : String
+setCreditCardExpirationDate(d : String)
+getCreditCardExpirationDate() : String
Java Servlets and JSPCH18
Download
Product
-user : User
-downloadDate: Date
-productCode : String
-code : String
-description: String
-price : double
+Download()
+Download(user : User,
date : Date,
code : String)
+setUser(user : User)
+getUser() : User
+setDownloadDate(date : Date)
+getDownloadDate() : Date
+setProductCode(code : String)
+getProductCode() : String
+Product()
+Product(code : String,
description : String,
price : double)
+seCode(c: String)
+getCode() : String
+setDescription(d : String)
+getDescription() : String
+setPrice(p : double)
+getPrice() : double
Invoice
-user : User
-lineItems : Vector
-invoiceDate : Date
-invoiceNumber : int
+Invoice()
+Invoice(user : User,
items : Vector,
date : Date,
num : int)
+setUser(user : User)
+getUser() : User
+setLineItems(items : Vector)
+getLineItems() : Vector
+setInvoiceDate(date : Date)
+getInvoiceDate() : Date
+setInvoiceNumber(num : int)
+getInvoiceNumber() : int
LineItem
-item : Product
-quantity: int
+LineItem()
+LineItem(product : Product,
quantity: int)
+setProduct(product: Product)
+getProduct() : Product
+setQuantity(qty : int)
+getQuantity() : int
+getTotalPrice() : double
Cart
-items : Vector
+Cart()
+Cart(lineItems : Vector)
+setItems(lineItems: Vector)
+getItems() : Vector
+addItem(item : LineItem)
+removeItem(item : LineItem)
© 2003, Mike Murach & Associates, Inc.
Slide 13
The Product class
package music.business;
public class Product{
private String code;
private String description;
private double price;
public Product(){}
public Product(String code, String description,
double price){
this.code = code;
this.description = description;
this.price = price;
}
public void setCode(String code){
this.code = code;
}
public String getCode(){ return code; }
Java Servlets and JSPCH18
© 2003, Mike Murach & Associates, Inc.
Slide 14
The Product class (continued)
public void setDescription(String description){
this.description = description;
}
public String getDescription(){ return description; }
public void setPrice(double price){
this.price = price;
}
public double getPrice(){ return price; }
}
Java Servlets and JSPCH18
© 2003, Mike Murach & Associates, Inc.
Slide 15
The database diagram
Product
Download
User
UserID
FirstName
LastName
EmailAddress
CompanyName
Address1
Address2
City
State
Zip
Country
CreditCardType
CreditCardNumber
CreditCardExpirationDate
users
user_name
user_pass
1:*
DownloadID
UserID
DownloadDate
ProductCode
*:1
ProductID
ProductCode
ProductDescription
ProductPrice
*:1
LineItem
Invoice
1:*
InvoiceID
UserID
InvoiceDate
TotalAmount
IsProcessed
1:*
LineItemID
InvoiceID
ProductID
Quantity
user_roles
1:*
Java Servlets and JSPCH18
user_role
role_name
© 2003, Mike Murach & Associates, Inc.
Slide 16
The SQL script for the database
DROP DATABASE IF EXISTS music;
CREATE DATABASE music;
USE music;
CREATE TABLE Product(
ProductID INT NOT NULL AUTO_INCREMENT,
ProductCode VARCHAR(10) NOT NULL DEFAULT '',
ProductDescription VARCHAR(100) NOT NULL DEFAULT '',
ProductPrice DECIMAL(7,2) NOT NULL DEFAULT '0.00',
PRIMARY KEY (ProductID),
FOREIGN KEY(ProductCode)
REFERENCES Downloads(ProductCode)
);
Java Servlets and JSPCH18
© 2003, Mike Murach & Associates, Inc.
Slide 17
The SQL script (continued)
CREATE TABLE User (
UserID INT NOT NULL AUTO_INCREMENT,
FirstName VARCHAR(50),
LastName VARCHAR(50),
EmailAddress VARCHAR(50),
CompanyName VARCHAR(50),
Address1 VARCHAR(50),
Address2 VARCHAR(50),
City VARCHAR(50),
State VARCHAR(50),
Zip VARCHAR(50),
Country VARCHAR(50),
CreditCardType VARCHAR(50),
CreditCardNumber VARCHAR(50),
CreditCardExpirationDate VARCHAR(50),
PRIMARY KEY (UserID)
);
Java Servlets and JSPCH18
© 2003, Mike Murach & Associates, Inc.
Slide 18
The SQL script (continued)
CREATE TABLE LineItem(
LineItemID INT NOT NULL AUTO_INCREMENT,
InvoiceID INT NOT NULL DEFAULT '0',
ProductID INT NOT NULL DEFAULT '0',
Quantity INT NOT NULL DEFAULT '0',
PRIMARY KEY (LineItemID),
FOREIGN KEY(UserID) REFERENCES User (UserID)
);
CREATE TABLE Invoice(
InvoiceID INT NOT NULL AUTO_INCREMENT,
UserID INT NOT NULL,
InvoiceDate DATETIME NOT NULL
DEFAULT '0000-00-00 00:00:00',
TotalAmount FLOAT NOT NULL DEFAULT '0',
IsProcessed enum('y','n') DEFAULT NULL,
PRIMARY KEY (InvoiceID),
FOREIGN KEY(UserID) REFERENCES User (UserID)
);
Java Servlets and JSPCH18
© 2003, Mike Murach & Associates, Inc.
Slide 19
The SQL script (continued)
CREATE TABLE Download (
DownloadID INT NOT NULL AUTO_INCREMENT,
UserID INT NOT NULL,
DownloadDate DATETIME NOT NULL,
ProductCode VARCHAR(10) NOT NULL,
PRIMARY KEY(DownloadID),
FOREIGN KEY(UserID) REFERENCES User (UserID)
);
INSERT INTO Product VALUES
('1', '8601', '86 (the band) - True Life Songs and
Pictures', '14.95'),
('2', 'pf01', 'Paddlefoot - The first CD', '12.95'),
('3', 'pf02', 'Paddlefoot - The second CD', '14.95'),
('4', 'jr01', 'Joe Rut - Genuine Wood Grained Finish',
'14.95');
Java Servlets and JSPCH18
© 2003, Mike Murach & Associates, Inc.
Slide 20
The SQL script (continued)
CREATE TABLE users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
INSERT INTO users VALUES ('andrea', 'dime10'),
('joel', '87band'),
('doug', 'lowe1');
CREATE TABLE user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
INSERT INTO user_roles VALUES ('andrea', 'service'),
('andrea', 'admin'),
('joel', 'admin');
Java Servlets and JSPCH18
© 2003, Mike Murach & Associates, Inc.
Slide 21
Class diagrams for the data access classes
ProductDB
+readProduct(connection : Connection, productCode : String) : Product
+readProductID(connection : Connection, product : Product) : int
+readRecord(connection : Connection, code: String) : Product
UserDB
+writeRecord(connection : Connection, user : User)
+overwriteRecord(connection: Connection, user : User) : boolean
+isMatch(connection : Connection, emailAddress : String) : boolean
+readUserID(connection : Connection, user : User) : int
+readRecord(connection : Connection, emailAddress : String) : User
InvoiceDB
+writeRecord(connection : Connection, invoice : Invoice)
LineItemDB
+writeRecord(connection : Connection, invoiceID : int, lineItem : LineItem)
+readRecords(connection : Connection, invoiceID : int) : Vector
DownloadDB
+addRecord(connection : Connection, download : Download)
Java Servlets and JSPCH18
© 2003, Mike Murach & Associates, Inc.
Slide 22
The ProductDB class
package music.data;
import java.sql.*;
import music.business.*;
public class ProductDB{
public static Product readRecord(Connection connection,
String productCode) throws SQLException{
String query = "SELECT * FROM Product " +
"WHERE ProductCode = '"
+ productCode + "'";
Statement statement = connection.createStatement();
ResultSet record = statement.executeQuery(query);
Product product = null;
boolean exists = record.next();
Java Servlets and JSPCH18
© 2003, Mike Murach & Associates, Inc.
Slide 23
The ProductDB class (continued)
if (exists){
int id = record.getInt("ProductID");
String code = record.getString("ProductCode");
String description =
record.getString("ProductDescription");
double price =
record.getDouble("ProductPrice");
product = new Product(code, description,
price);
}
record.close();
statement.close();
return product;
}
Java Servlets and JSPCH18
© 2003, Mike Murach & Associates, Inc.
Slide 24
The ProductDB class (continued)
public static int readProductID(Connection connection,
Product product) throws SQLException{
String query = "SELECT ProductID FROM Product " +
"WHERE ProductCode = '"
+ product.getCode() + "'";
Statement statement = connection.createStatement();
ResultSet record = statement.executeQuery(query);
record.next();
int productID = record.getInt("ProductID");
record.close();
statement.close();
return productID;
}
Java Servlets and JSPCH18
© 2003, Mike Murach & Associates, Inc.
Slide 25
The ProductDB class (continued)
public static Product readRecord(Connection connection,
int productID) throws SQLException{
String query = "SELECT * FROM Product " +
"WHERE ProductID = '"
+ productID + "'";
Statement statement = connection.createStatement();
ResultSet record = statement.executeQuery(query);
Product product = null;
boolean exists = record.next();
if (exists){
String code = record.getString("ProductCode");
String description =
record.getString("ProductDescription");
double price = record.getDouble("ProductPrice");
product = new Product(code, description, price);
}
record.close();
statement.close();
return product;
}
}
Java Servlets and JSPCH18
© 2003, Mike Murach & Associates, Inc.
Slide 26