Java开发工程师(Web方向) - 01.Java Web开发入门 - 第1章.Web应用开发概述

时间:2023-11-26 13:18:08

第1章--Web应用开发概述

Web应用开发概述

浏览器-服务器架构(BS-architecture)

browser/ App    ---- request ---->    server (database)

<- response/data --

protocol: HTTP(Hyper-Text Transfer Protocol)

have a try:

cmd: sudo nc -l 80  -- listen at the port of 80(port 80 is used by HTTP)

browser: type "localhost" in the address bar

these are the info that sent to the server(localhost)

Java开发工程师(Web方向) - 01.Java Web开发入门 - 第1章.Web应用开发概述

meanwhile, the browser is waiting for the request (from the server)

try to type "hello" and ctrl+c

Java开发工程师(Web方向) - 01.Java Web开发入门 - 第1章.Web应用开发概述

"hello" will be displayed in the browser

Java开发工程师(Web方向) - 01.Java Web开发入门 - 第1章.Web应用开发概述

If we enter the address of a website, such as "http://study.163.com"

it is called URL (uniform resource locator) (统一资源定位)

In this case: "http://study.163.com/smartSpec/intro.html?name=163"

http: protocol, such as ftp, mailto

smartSpec/intro.html: request sent to the server

?name=163: data that sent to the server via GET method

Let's see what info can be sent via GET method

[html] view plain copy

    1. <html> <body>
    2. <form action="http://localhost/form_action.html" method="get">
    3. <p>First Name: <input type="text" name="fname" /></p>
    4. <p>Last Name: <input type="text" name="lname" /></p>
    5. <input type="submit" value="Submit" />
    6. </form> </body> </html>

1. open .html in the browser

2. type sudo nc -l 80 in terminal

3. submit the info

4. result

GET /form_action.html?fname=Lin&lname=Matt

Java开发工程师(Web方向) - 01.Java Web开发入门 - 第1章.Web应用开发概述

What if we use POST as the method?

same steps as GET

but different result:

POST /form_action.html (no extra data shown explicitly after "/form_action.html")

but "fname=Lin&lname=Matt" will be displayed at the end.

furthermore, new property "content-length" added, which means the number of characters (data) sent to the server, which is fname=Lin&lname=Matt in this case)

Java开发工程师(Web方向) - 01.Java Web开发入门 - 第1章.Web应用开发概述

After those info/data/request(shown above) has been sent to the server,

the server needs to give the feedback(response) back to the client/ browser.

feedback:

1. 静态页面(such as .html file, .img file)

2. 动态数据  (data retrieved by programs running on the server side based on the input (like first name/ last name in the previous example))

HTTP protocol: connectionless 无连接的

After a series of actions has done (request --> response),the connection gets broken.

Advantages --> disconnect with those users who are not currently communicating with the server at that time, in other words, accessible to as many users as possible at the same time

Disadvantages: the server does not know who u r after one connection.

for example, after we log in, we expect that we do not need to log in again if we try to visit other relevant webpages.

--> solution: cookie

after the first-time connection, the client got a "number" from the server which identifies the client/ browser itself.

then the browser will store that number, and every time the client connects to the server, it will bring that number with it, and the server will know the client by checking that id number

that number in this example is called cookie.

Also, the cookie got its own expiry date, which is set by the server.

HTTP会话 (session) :connection, request, response, close.

HTTP protocol: how to deal with the data in the database

background: when the client sends a request to the server, the program in the server always try to retrieve the relevant data from its database system, and then the program is going to generate an HTML file (or other file that can be understood by the browser), and sends back to the client as a response.

problem 1: the limitation of the number of connections between the database and the web server is much smaller than that between the client and the web server.

in other words, minimising the number of the connections btw database and the web server is required.

--> solution1: connection pool

creates a connection pool between the web server and the database which has limited connections.

each time the web server requires a connection to the database, it will ask the connection pool for the access of connection

--> solution2: cache

different clients might search for the same data during a short period of time

there is no need to run the program to retrieve the same data from the database repeatedly.

-- lower the workload of the database and speed up the reaction from the server