how tomcat works 1 simple web server
2019獨角獸企業重金招聘Python工程師標準>>>
HTTP協議的主要特點可概括如下:
1.支持客戶/服務器模式。
2.簡單快速:客戶向服務器請求服務時,只需傳送請求方法和路徑。請求方法常用的有GET、HEAD、POST。每種方法規定了客戶與服務器聯系的類型不同。由于HTTP協議簡單,使得HTTP服務器的程序規模小,因而通信速度很快。
3.靈活:HTTP允許傳輸任意類型的數據對象。正在傳輸的類型由Content-Type加以標記。
4.無連接:無連接的含義是限制每次連接只處理一個請求。服務器處理完客戶的請求,并收到客戶的應答后,即斷開連接。采用這種方式可以節省傳輸時間。
5.無狀態:HTTP協議是無狀態協議。無狀態是指協議對于事務處理沒有記憶能力。缺少狀態意味著如果后續處理需要前面的信息,則它必須重傳,這樣可能導致每次連接傳送的數據量增大。另一方面,在服務器不需要先前信息時它的應答就較快。
請求方法(所有方法全為大寫)有多種,各個方法的解釋如下:
GET???? 請求獲取Request-URI所標識的資源
POST??? 在Request-URI所標識的資源后附加新的數據
HEAD??? 請求獲取由Request-URI所標識的資源的響應消息報頭
PUT???? 請求服務器存儲一個資源,并用Request-URI作為其標識
DELETE? 請求服務器刪除Request-URI所標識的資源
TRACE?? 請求服務器回送收到的請求信息,主要用于測試或診斷
CONNECT 保留將來使用
OPTIONS 請求查詢服務器的性能,或者查詢與資源相關的選項和需求
HTTP Request
由3部分組成:
- Method URI (Uniform resource identifier) protocol/version
- Request headers
- Entity body
Http Request 例子:
POST /examples/default.jsp HTTP/1.1 //請求方法 統一資源定位符 協議/版本Accept: text/plain; text/html Accept-Language: en-gb Connection: Keep-Alive Host: localhost User-Agent: Mozilla/4.0 (compatible; MSIE 4.01; Windows 98) Content-Length: 33 Content-Type: application/x-www-form-urlencoded Accept-Encoding: gzip, deflate // 頭lastName=Franks&firstName=Michael //實體 HTTP Responses?
也是由3部分組成:
- 協議版本 狀態 描述
- 響應頭
- 實體
響應的例子:?
HTTP/1.1 200 OK Server: Microsoft-IIS/4.0 Date: Mon, 5 Jan 2004 13:13:33 GMT Content-Type: text/html Last-Modified: Mon, 5 Jan 2004 13:13:12 GMT Content-Length: 112 <html> <head> <title>HTTP Response Example</title> </head> <body> Welcome to Brainy Software </body> </html>The Socket Class
Socket 是網絡連接的終端,它可以用來實現程序對網絡資源的讀寫。創建一個Socket
public Socket(java.lang.String host, int port)
例如連接到yahoo, new Socket("yahoo",80);
如下代碼創建了一個套接字,來與本地的HTTP服務器進行通信,并接受服務器的響應。?
?
Socket socket = new Socket("127.0.0.1", "8080"); OutputStream os = socket.getOutputStream(); boolean autoflush = true; PrintWriter out = new PrintWriter( socket.getOutputStream(), autoflush); BufferedReader in = new BufferedReader( new InputStreamReader( socket.getInputstream() )); // send an HTTP request to the web server out.println("GET /index.jsp HTTP/1.1"); out.println("Host: localhost:8080"); out.println("Connection: Close"); out.println(); // read the response boolean loop = true; StringBuffer sb = new StringBuffer(8096); while (loop) { if ( in.ready() ) { int i=0; while (i!=-1) { i = in.read(); sb.append((char) i); } loop = false; } Thread.currentThread().sleep(50); } // display the response to the out console System.out.println(sb.toString()); socket.close(); The ServerSocket classnew ServerSocket(8080, 1, InetAddress.getByName("127.0.0.1"));
The Application
我們的服務器端程序由3部分組成:
- HttpServer ?
- Request ?
- Response ?
The HttpServer class
這個web服務器可以提供靜態資源服務。?
public static final WEB_ROOT and all subdirectories under it. WEB_ROOT is?
initialized as follows:?
public static final String WEB_ROOT =?? System.getProperty("user.dir") + File.separator + "webroot";?
這行代碼列出webroot的目錄,包含許多靜態資源,如果想請求一個靜態資源,你在瀏覽器中可以輸入?
http://machineName:port/staticResource 例如:http://localhost:8080/index.html?
The Request Class
The Response class
Run the application
在瀏覽器中輸入http://localhost:8080/index.html ?
在控制臺,你會看到:
GET /index.html HTTP/1.1?
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,?
application/vnd.ms-excel, application/msword, application/vnd.ms-?
powerpoint, application/x-shockwave-flash, application/pdf, */*?
Accept-Language: en-us?
Accept-Encoding: gzip, deflate?
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR?
1.1.4322)?
Host: localhost:8080?
Connection: Keep-Alive?
?
GET /images/logo.gif HTTP/1.1?
Accept: */*?
Referer: http://localhost:8080/index.html?
Accept-Language: en-us?
Accept-Encoding: gzip, deflate?
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR?
1.1.4322)?
Host: localhost:8080?
Connection: Keep-Alive?
?
轉載于:https://my.oschina.net/sunxuetao/blog/126853
總結
以上是生活随笔為你收集整理的how tomcat works 1 simple web server的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据库书籍大师推荐的Oracle数据库相
- 下一篇: [笔试面试][code_by_hand]