Bài giảng Lập trình Web - Bài 2: Kiến trúc Web
Kiến trúc Web
Nội dung
Kiến trúc web
Giao thức HTTP
Mô hình MVC
Kiến trúc Web
Kiến trúc Web Platform
. Platform
• Hardware
• Software modules & configurations
• Choice of software platform (e.g., J2EE, .NET)
Kiến trúc Web Application
. Mô hình hóa các tiến trình xử lý
. Trong một lĩnh vực cụ thể
. Module hóa và phức tạp
Kiến trúc Web Platform
Web platform dựa trên các thành phần sau
. TCP/IP
. HTTP
. HTML
Mô hình kiến trúc Client/Server
Là mô hình phức tạp…
. Các thành của network (firewall, proxy, load
balancer)
. Các thành phần của intranet (web server,
application server, database, web services)
Mô hình Web (tầng ứng dụng)
Các thành phần ngữ nghĩa
của Web
Dịch vụ Web: Nội dung
động và có tính tương tác
. PHP, Python, Java
. URLs/URIs
. HTTP
. HTML
HTTP for
transport
HTML/XHTML
for display
HTTP
Server
URL/URI for
addressing
PHP, python etc.
for interaction
Clients
(browsers)
Mô hình Web (các tầng giao thức)
Client
Web server
HTTP message
TCP segment
HTTP
HTTP
TCP
IP
TCP
IP
Router
Router
IP packet
IP packet
IP packet
IP
IP
Ethernet
interface
Ethernet
interface
SONET
interface
SONET
interface
Ethernet
interface
Ethernet
interface
Ethernet
SONET link
Ethernet
Giao thức HTTP
Những bước xử lý của browser
http://origin/..
DNS query
DNS
server
Client
Origin
server
Establish TCP connection
HTTP request
HTTP
transaction
HTTP response
Optional parallel connections
HTML
Giao dịch của HTTP
Một giao dịch của giao thức HTTP là một request và
một response (một phản hồi của một request nào đó)
giữa web client và web server
GET / HTTP/1.1
Accept: image/gif, image/x-xbitmap,
image/ jpeg, image/pjpeg, */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible;
Client
MSIE 5.01; Windows NT)
Connection: Keep-Alive
Origin
server
HTTP/1.1 200 OK
Date: Mon, 15 Jul 2002 08:49:00 GMT
Server: Apache/1.3.26 (Unix) PHP/4.2.1
Last-Modified: Wed, 12 Jun 2002
08:49:49 GMT
ETag: "2a-50ea-3d070b2d"
Accept-Ranges: bytes
Content-Length: 20714
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: text/html
<html>
...
HTML
HTTP Message
HTTP message ở dạng ASCII text, gồm:
. Một dòng yêu cầu
Method Request-URI HTTP-Version (Request-Line)
HTTP-Version Status-Code Reason-Phrase (Status-Line)
. Các trường Header
field-name ":" [ field-value ]
. Một dòng trống <CR>
. Phần thân thông báo (tùy chọn)
HTTP Request
Ví dụ
GET /foo.html HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/ jpeg, image/pjpeg, */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT)
Connection: Keep-Alive
<CR>
GET /foo.html HTTP/1.1— request "/foo.html" using HTTP version 1.1
Accept: — types of documents accepted by browser
Accept-Language: — preferred language is english
Accept-Encoding: — browser understands compressed documents
User-Agent: — identification of browser (real type is IE 5.01)
Host: — what the client thinks the server host is
Connection: — keep TCP connection open until explicitly disconnected
HTTP Response
Ví dụ:
HTTP/1.1 200 OK
Date: Mon, 15 Jul 2002 08:49:00 GMT
Server: Apache/1.3.26 (Unix) PHP/4.2.1
Last-Modified: Wed, 12 Jun 2002 08:49:49 GMT
ETag: "2a-50ea-3d070b2d"
Accept-Ranges: bytes
Content-Length: 20714
Connection: close
Content-Type: text/html
<CR>
<html>...
HTTP/1.1 200 OK — document found (code 200); server is using HTTP 1.1
Date: — current date at the server
Server: — software run by the server
Last-Modified: — most recent modification of the document
ETag: — entity tag (unique identifier for the server resource, usable for caching)
Accept-Ranges: — server can return subsections of a document
Content-Length: — length of the body (which follows the header) in bytes
Connection: — the connection will close after the server's response
Content-Type: — what kind of document is included in the response
<html>... — document text (follows blank line)
Client Methods
GET
DELETE
Retrieve a resource from the
server (static file, or
Remove a resource from the
server
dynamically-generated data)
OPTIONS (HTTP 1.1)
HEAD
Request other options for an
Get information about a
resource (but not the actual
resource)
URI (methods understood by a
server or allowed for a given
URI)
POST
TRACE (HTTP 1.1)
Client provides some
Ask proxies to declare
themselves in the headers
(used for debugging)
information to the server, e.g.,
through forms (may update
the state of the server)
CONNECT (HTTP 1.1)
PUT
Used for HTTPS (secure
HTTP) through a proxy
Provide a new or replacement
resource to put on the server
HTTP Caching
Dữ liệu Cache gần người dùng hơn
. Improves Web performance, reduces load on server
. Cache control directives in HTTP header (no cache, age, etc.)
Cache
Origin
GET http://origin/..
server
server
Client 1
GET http://origin/..
200 OK
200 OK
Client 2
GET http://origin/..
200 OK
HTTP Client Authentication
Cơ chế bảo mật đơn giản thông qua username/password
. Basic scheme: username:passwordbase-64 encoded
echo -n “user:password" | openssl base64
echo "c2NvdHQ6dGlnZXI=" | openssl base64 -d
GET /private/ HTTP/1.1
Client
Origin
server
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Basic realm="secret"
Username:joe
Password:********
GET /private/ HTTP/1.1
Authorized: Basic SHY3GH7D3SH==
HTTP/1.1 200 OK
...
HTML
Cookies
Cho phép Web servers lưu
trạng thái của Client
. Dùng cặp khóa – giá trị
Client
. Thường dùng quản lý các
Origin
server
phiên giao dịch session
GET http://origin/..
. Cookie có thể truy xuất bởi
200 OK
Set-Cookie: ABC=XYZ
một site/domain
. Server có thể định thời gian
hết hạn
GET http://origin/..
Cookie: ABC=XYZ
. Client có thể từ chối cookie,
giới hạn kích thước, thời
gian…
200 OK
Mô hình: Model View Controller
Mô hình được sử dụng trong Smalltalk
(1979)
Tách riêng giữa tầng dữ liệu và hiển thị
Dễ dàng phát triển
Mô hình: Model View Controller
Model
. Tượng trưng cho dữ liệu trong phần mềm
ứng dụng
View
. Tầm nhìn hay khung nhìn (view) bao gồm các
thành phần của giao diện người dùng
Controller
. Quản lý sự trao đổi dữ liệu
. Các nguyên tắc nghề nghiệp trong các thao
tác liên quan đến mô hình
Model-View-Controller 2 cho
Web
Sự thích ứng của MVC đối với Web
. Kết nối không bền vững giữa client và server
. Thông báo những thay đổi
. Truy vấn lại server để phát hiện những thay
đổi
Client/Server (2-Layer)
Client
Client
Server
Web/App Server
Database
Services
Dynamic HTML
Static HTML
Client/Server (2-Layer)
Tải về để xem bản đầy đủ
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Lập trình Web - Bài 2: Kiến trúc Web", để tải tài liệu gốc về máy hãy click vào nút Download ở trên
File đính kèm:
- bai_giang_lap_trinh_web_bai_2_kien_truc_web.pdf