CSCI4140 3 - Web-App
CSCI4140 3 - Web-App
https://course.cse.cuhk.edu.hk/~csci4140/
Web Application Architecture
Web Server /
Web Browser Storage System
Application Server
HTTP
Internet LAN
Python
2
Hypertext Markup language
• Markup Language
指導的
- Include directives with content
3
HTML Tags
• Web browsers use HTML tags to render a web page
-
A 也
4
而
Example of HTML Document
了 textwntmt
<!DOCTYPE html> < ! DOCTYPEhtml >
Introduction
Welcome to my page!
5
〉
Attributes of HTML Tags
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>Introduction</h1>
<p id="p1" style="color: red">Welcome to my page!</p>
—
=
> "
p
</html>
Introduction
Welcome to my page!
6
Cascading Style Sheet (CSS)
• Describes the style of an HTML document
Children
→
parnt → grand cnildreu
-
?
8
塌州像世 7
Using CSS
Advantages
8
Example of CSS
<!DOCTYPE html> body {
<html> background-color: lightblue;
<head> }
<title>Page Title</title>
</head> h1 {
<body> color: white;
text-align: center;
<h1>Introduction</h1> } Selector
<p>Welcome to my page!</p> seleetor
p {
</body>
</html>
Declaration
Block
{ font-family: verdana;
font-size: 24px;
} Property Value
Dedaratin
property ralue
Bot
HTML CSS
Introduction
Welcome to my page!
9
CSS Selectors: .class And #id
<!DOCTYPE html> body {
<html> background-color: lightblue;
<head> }
<title>Page Title</title>
</head> .white-center {
<body> color: white;
<h1 class=“white-center” text-align: center;
>Introduction</h1> }
</body>
</html> HTML CSS
Introduction
Welcome to my page!
This paragraph is centered.
10
JavaScript
• The programming language of HTML and the Web
11
Web Application Architecture
Web Server /
Web Browser Storage System
Application Server
HTTP
Internet LAN
12
Hypertext Transport Protocol (HTTP)
http://www.example.com/index.html
get
• The browser fetches the file index.html from the server
using the HTTP protocol
13
西
HTTP Request
• Writes data into socket
14
HTTP Response
• Reads data from socket
…
Version Status Status Message
HTTP/1.1 200 OK
Date: Sun, 7 Jul 2018 17:36:27 GMT
Server: Apache
Headers Content-Encoding: gzip
Content-Type: text/html; charset=UTF-8
Content-Length: 1846
\r\n
<!DOCTYPE html>
<html>
Body
…
</html>
15
Common HTTP Response Status
Codes
Code Status Description
200 OK Success
301 Moved Permanently Permanent redirection
307 Temporary Redirect Try URI in the Location field this time
16
HTTP Methods
• GET - Reads data from a specified resource
17
HTTP Is Stateless
• The HTTP server is not required to retain information or
status about each user for the duration of multiple
requests.
- HTTP cookies
18
Web Application Architecture
Web Server /
Web Browser Storage System
Application Server
HTTP
Internet LAN
19
Web Servers
• Both Web Browsers and Web Servers speak HTTP
int fd = open(“index.html”);
{ int len = read(fd, buffer, sizeOfFile(fd));
write(tcpConnection, httpResponseHeader, headerSize);
} write(tcpConnection, buffer, len);
close(fd);
:
④
⑤
21
Generating Dynamic Content
• Common Gateway Interface (CGI)
協議
- A protocol for web servers to execute programs like
calling Command-line Interface (CLI) programs (in
another process) to generate web pages dynamically
22
How CGI Programs Work?
比功課 input mvivonment variable
#!/usr/bin/perl
=head1 DESCRIPTION
=cut
print "Content-type: text/plain\n\n";
https://csci4140.cse.cuhk.edu.hk/cgi-bin/env.pl
23
、
生
金
Partial Response Generated by CGI
Program
Version Status Status Message
HTTP/1.1 200 OK
Date: Sun, 7 Jul 2018 17:36:27 GMT Server Output
Server: Apache
Content-Encoding: gzip
Headers
Content-type: text/plain
\r\n
CONTENT_LENGTH = "" CGI Program Output
CONTENT_TYPE = ""
Body DAEMON_OPTS = "-f" CGI Program should generate
DOCUMENT_ROOT = "/var/www/html" • The content-type
… • The body
Discussion
How can you return a zip file so that the browser knows that it is a zip file?
24
Baseonconenttype
VS Static
Dynamic Content
四
- Form submission
- Search
- …
25
User Input - GET Request
<form method="GET" action="form.cgi">
Username: alex
<input type="text" name="user"></input><br> enUS
Language:
<input type="text" name="lang"></input><br>
<input type="submit"></input>
</form> ? eparmd fom URL
可
http://www.example.com/form.cgi?user=alex&lang=enUS Query String
在parate
Method URL Version kay vulmepaivs
以
POST /form.cgi HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0
Headers Accept: text/html, */*
Accept-Language: en-us
Content-Type: application/x-www-form-urlencoded
Content-length: 19
\r\n
Body user=alex&lang=enUS
27
GET Request vs. POST Request
GET POST
28
Problems of CGI
• High performance overhead
29
Reducing Overhead
• FastCGI