日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

how tomcat works 1 simple web server

發布時間:2025/5/22 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 class

new ServerSocket(8080, 1, InetAddress.getByName("127.0.0.1"));

The Application

我們的服務器端程序由3部分組成:

  • HttpServer ?
  • Request ?
  • Response ?

The HttpServer class


Listing 1.1: The HttpServer class package ex01.pyrmont; import java.net.Socket; import java.net.ServerSocket; import java.net.InetAddress; import java.io.InputStream; import java.io.OutputStream; import java.io.IOException; import java.io.File; public class HttpServer { /** WEB_ROOT is the directory where our HTML and other files reside. * For this package, WEB_ROOT is the "webroot" directory under the * working directory. * The working directory is the location in the file system * from where the java command was invoked. */ public static final String WEB_ROOT = System.getProperty("user.dir") + File.separator + "webroot"; // shutdown command private static final String SHUTDOWN_COMMAND = "/SHUTDOWN"; // the shutdown command received private boolean shutdown = false; public static void main(String[] args) { HttpServer server = new HttpServer(); server.await(); } public void await() { ... } } Listing 1.2: The HttpServer class's await method public void await() { ServerSocket serverSocket = null; int port = 8080; try { serverSocket = new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1")); } catch (IOException e) { e.printStackTrace(); System.exit(1); } // Loop waiting for a request while (!shutdown) { Socket socket = null; InputStream input = null; OutputStream output = null; try { socket = serverSocket.accept(); input = socket.getInputStream(); output = socket.getOutputStream(); // create Request object and parse Request request = new Request(input); request.parse(); // create Response object Response response = new Response(output); response.setRequest(request); response.sendStaticResource(); // Close the socket socket.close(); //check if the previous URI is a shutdown command shutdown = request.getUri().equals(SHUTDOWN_COMMAND); } catch (Exception e) { e.printStackTrace (); continue; } } }


這個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


Listing 1.3: The Request class package ex01.pyrmont; import java.io.InputStream; import java.io.IOException; public class Request { private InputStream input; private String uri; public Request(InputStream input) { this.input = input; } public void parse() { ... } private String parseUri(String requestString) { ... } public String getUri() { return uri; } }


Listing 1.4: The Request class's parse method public void parse() { // Read a set of characters from the socket StringBuffer request = new StringBuffer(2048); int i; byte[] buffer = new byte[2048]; try { i = input.read(buffer); } catch (IOException e) { e.printStackTrace(); i = -1; } for (int j=0; j<i; j++) { request.append((char) buffer[j]); } System.out.print(request.toString()); uri = parseUri(request.toString()); } Listing 1.5: the Request class's parseUri method private String parseUri(String requestString) { int index1, index2; index1 = requestString.indexOf(' '); if (index1 != -1) { index2 = requestString.indexOf(' ', index1 + 1); if (index2 > index1) return requestString.substring(index1 + 1, index2); } return null; }


The Response class


Listing 1.6: The Response class package ex01.pyrmont; import java.io.OutputStream; import java.io.IOException; import java.io.FileInputStream; import java.io.File; /* HTTP Response = Status-Line *(( general-header | response-header | entity-header ) CRLF) CRLF [ message-body ] Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF */ public class Response { private static final int BUFFER_SIZE = 1024; Request request; OutputStream output; public Response(OutputStream output) { this.output = output; } public void setRequest(Request request) { this.request = request; } public void sendStaticResource() throws IOException { byte[] bytes = new byte[BUFFER_SIZE]; FileInputStream fis = null; try { File file = new File(HttpServer.WEB_ROOT, request.getUri()); if (file.exists()) { fis = new FileInputStream(file); int ch = fis.read(bytes, 0, BUFFER_SIZE); while (ch!=-1) { output.write(bytes, 0, ch); ch = fis.read(bytes, 0, BUFFER_SIZE); } } else { // file not found String errorMessage = "HTTP/1.1 404 File Not Found\r\n" + "Content-Type: text/html\r\n" + "Content-Length: 23\r\n" + "\r\n" + "<h1>File Not Found</h1>"; output.write(errorMessage.getBytes()); } } catch (Exception e) { // thrown if cannot instantiate a File object System.out.println(e.toString() ); } finally { if (fis!=null) fis.close(); } } }

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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。