Example Online Databases • https://www.planespotters.net/ • https://www.comics.org/ • https://www.quandl.com/ • https://www.enigma.com/ • https://www.sportradar.com/
Basics of WWW • The Web is a very large client-server system — Connected through routers and switches — Communicating with TCP/IP protocol — With no centralised control • Servers publish pages at URLs • Clients request pages by specifying the URLs • Pages are transferred on the Web using HTTP protocol • Each HTTP interaction is independent — No concept of a state or session WWW基本原理 网络是大客户端服务器系统 通过路由器和交换机连接 使用 TCP/IP 协议通信 - 没有集中控制 - 服务器在 URL 上发布网页 - 客户端通过指定 URL 请求网页 - 使用 HTTP 协议在网络上传输网页 - 每次 HTTP 交互都是独立的 - 没有状态或会话的概念
1990’s Client-Server DBMS Architecture • Example: A high street travel agency • Application/business code on client machine – “fat client” • Proprietary software – expensive to maintain & update 1990客户端五五七DBMS架构客户机上发代码程序 专有软件 维护更新成本高From 1995: Three-Tier Architecture三层架构 • Example: A web-based bookstore. e.g., amazon.co.uk • All application/business code off client, onto server 所有代码程序从客户端移到服务器
Characteristics of Three-Tier Architecture • Advantages: — Platform independence – web browsers for every PC — Cheap graphical user interface – potential for innovation — Simplicity – easier to upgrade & scale • Disadvantages: — Costly to maintain server – poor development tools — Less secure (hackers, etc.) — Less reliable (packet loss) — Stateless – no built-in support for transactions — Scaling limitations – size of data and No. of users
优点: - 平台独立性 - 每台 PC 都有网络浏览器 - 廉价的图形用户界面 - 创新潜力 - 简化 - 升级和扩展更容易 - 缺点: - 维护服务器成本高 - 开发工具差 - 安全性较差(黑客等) - 可靠性较低(数据包丢失) - 无状态 没有内置的事务支持 - 扩展限制 数据大小和用户数量
University Architecture
Static and Dynamic Content • HTML content stored entirely in files is static • Most Web content is dynamic — needs to vary with time and users — e.g., Amazon.co.uk • Dynamic HTML pages need to be generated for every transfer/access • Dynamic content may come from — user inputs — statements / procedures — database tables • Linking databases to web involves creating HTML pages on the fly using database query results • We learn some techniques to generate dynamic HTML pages静态内容和动态内容 - 完全存储在文件中的 HTML 内容是静态的 - 大多数网络内容是动态的 - 需要随时间和用户而变化 例如 Amazon.co.uk - 每次传输/访问都需要生成动态 HTML 页面 - 动态内容可能来自 - 用户输入 语句/程序 数据库表 - 将数据库链接到网络需要使用数据库查询结果即时创建 HTML 页面
Web Server Software Old Technologies • Existing Java programs that connect to DBMS can be extended to generate dynamic HTML using CGI • CGI = Common Gateway Interface • CGI is generic and can be used with — Java, C and other programming languages — Unix scripts and other scripting languages • Low-level DB access exploits DB interface libraries such as JDBC • When using CGI with Java — User input is obtained from HTML forms — CGI script is invoked when user submits the form (see the html form in the example) — CGI passes user input from the html form to the Java class ◦ as a list of attribute value pairs separated by ‘&’网络服务器软件 旧技术 - 连接 DBMS 的现有 Java 程序可通过 CGI 扩展到生成动态 HTML - CGI = 通用网关接口 - CGI 是通用的,可用于 - Java、 C 和其他编程语言 Unix 脚本和其他脚本语言 - 底层 DB 访问利用 DB 接口库(如 JDBC) - 在 Java 中使用 CGI 时 - 用户输入从 HTML 表单中获取 用户提交表单时调用 CGI 脚本(见示例中的 html 表单) CGI 将用户输入从 html 表单传递到 Java 类 ◦,作为以"&"分隔的属性值对列表。
Example – CGI scripting • This example contains — An HTML form to obtain user name as input — A CGI script for invoking a java class — A Java class ◦ For processing the user input and ◦ Printing a ‘Hello World’ message • Example only illustrates how cgi facilitates linking — HTML forms and Java classes — Java class does not really connect to a database — Try adding code for connecting to a database本示例包含 - 一个 HTML 表单,用于获取用户名作为输入 一个 CGI 脚本,用于调用一个 Java 类 一个 Java 类 ◦ 用于处理用户输入和 ◦ 打印一条 "Hello World "消息 - 示例仅说明了 cgi 如何促进链接 - HTML 表单和 Java 类 Java 类并不真正连接到数据库 尝试添加连接到数据库的代码
Example – HTML Form
Java Code
CGI Script • Only two lines in ‘helloworld.cgi’ file
• Each CGI program runs as a separate process – resource-intensive • Difficult to mix content and functionality — As in the example you need to create the output HTML file in the Java class — Not easy at times • Warning: each server is configured differently for running CGI scripts — Speak to the sys admin每个 CGI 程序都作为单独的进程运行 资源密集型 - 难以混合内容和功能 -需要在 Java 类中创建输出 HTML 文件 有时并不容易 -每个服务器对运行 CGI 脚本的配置不同 需要咨询系统管理员
Web Server Software New Technologies • There are many new technologies that allow access to DBMSs • Naturally there are advantages and disadvantages associated with each of them • Examples — Microsoft IIS, ASP – JScript/VBScript, Ajax — Sun Microsystems Java – JSP, servlets — Mozilla Firefox – JavaScript — ... • In this course we introduce you to: — PHP code embedded in HTML to access MySQL databases — Ruby code to access SQLite databases — Python code to access MongoDB databases网络服务器软件新技术 - 有许多新技术可以访问数据库管理系统 - 每种技术都有其优缺点 - 示例 - Microsoft IIS、ASP - JScript/VBScript、Ajax - Sun Microsystems Java - JSP、servlets - Mozilla Firefox - JavaScript - ... - 重点 - 嵌入 HTML 的 PHP 代码访问 MySQL 数据库 Ruby 代码访问 SQLite 数据库 Python 代码访问 MongoDB 数据
PHP & MySQL • Stands for PHP: Hypertext Preprocessor — Recursive acronym • Is a scripting language — Interpreted, not compiled — Public domain software • Embedded directly into HTML pages — Pages are published with “.php” extension • Server executes the embedded PHP code every time that page is requested • Home Page: www.php.net • PHP+MySQL is a very popular combination for producing dynamic web pages PHP 的缩写: 超文本预处理器 递归首字母缩写 - 是一种脚本语言 - 解译,而非编译 - 公有软件 直接嵌入 HTML 页面 页面以".php "扩展名发布 - 每次请求该页面时,服务器都会执行嵌入的 PHP 代码 - 主页:www.php.net - PHP+MySQL 是制作动态网页非常流行的组合方式
Web Database Architecture with PHP and MySQL 1. Browser issues an HTTP request for a particular web page 2. Web server receives the request, retrieves the file and passes it to the PHP engine for processing 3. PHP engine connects to the MySQL server and sends the query 4. MySQL server receives the query, processes it, and sends the results back to the PHP engine 5. PHP engine receives the results, prepares the HTML page and send it to the web server 6. Web server sends the HTML page to the browser and browser displays the page to the user使用 PHP 和 MySQL 的网络数据库架构 1. 浏览器对特定网页发出 HTTP 请求 2. 网络服务器接收请求,检索文件并将其传递给 PHP 引擎进行处理 3. PHP 引擎连接到 MySQL 服务器并发送查询 4. 4. MySQL 服务器接收查询,进行处理,并将结果发送回 PHP 引擎 5. PHP 引擎接收结果,准备 HTML 页面并将其发送至网络服务器 6. 网络服务器将 HTML 页面发送到浏览器,浏览器将页面显示给用户
PHP+MySQL Programming Model • Web site made from “.php” files on web server • “.php” files contain HTML with embedded PHP code • PHP code is enclosed in
• Basic steps followed in any PHP script used to access a database: — Check and filter data coming from the user — Set up a connection to MySQL server — Selecting the appropriate database — Query the database — Retrieve the results — Present the results back to the user — Close the database connectionPHP+MySQL 编程模型 - 网站由网站服务器上的".php "文件组成 - ".php "文件包含嵌入了 PHP 代码的 HTML - PHP 代码被括入 <?php ... ?> - 任何用于访问数据库的 PHP 脚本都遵循以下基本步骤: - 检查和过滤来自用户的数据 建立与 MySQL 服务器的连接 选择适当的数据库 查询数据库 检索结果 将结果返回给用户 关闭数据库连接
Sample PHP Code
Recap: Building Web database Applications • Apply appropriate software engineering life cycle — Requirements analysis — Design — Implementation — Testing • Security of data is very important in Web Database applications • Use MySQL privilege system to control access to data • User identification and personalization is necessary with web database applications构建网络数据库应用程序--应用适当的软件工程生命周期--需求分析 设计 实施 测试--数据安全在网络数据库应用程序中非常重要--使用 MySQL 权限系统控制数据访问--网络数据库应用程序需要用户识别和个性化
Summary • Organizations increasingly want data to be available over the internet • Web databases require new technologies to extend simple HTTP protocol used on the web • PHP is a scripting language embedded in html code to develop web database applications • Security is one of the main issues in web database applications • Useful Links — www.w3.org – World Wide Web Consortium — www.wdvl.com – Web Developer’s Virtual Library — www.php.net – PHP home page — www.mysql.com – MySQL home page
各组织越来越希望通过互联网提供数据 - 网络数据库需要新技术来扩展网络上使用的简单 HTTP 协议 - PHP 是一种嵌入 html 代码的脚本语言,用于开发网络数据库应用程序 - 安全性是网络数据库应用程序的主要问题之一