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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Tomcat源码学习(7)-How Tomcat works(转)

發布時間:2025/3/14 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Tomcat源码学习(7)-How Tomcat works(转) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Response

??? ex01.pyrmont.Response類代表一個HTTP響應,在Listing 1.6里邊給出。
???????? Listing 1.6: Response


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();
???????? }
???? }
}

??? 首先注意到它的構造方法接收一個java.io.OutputStream對象,就像如下所示。

public Response(OutputStream output) {
???? this.output = output;
}

??? 響應對象是通過傳遞由套接字獲得的OutputStream對象給HttpServer類的await方法來構造的。Response類有兩個公共方法:setRequestsendStaticResourcesetRequest方法用來傳遞一個Request對象給Response對象。
??? sendStaticResource
方法是用來發送一個靜態資源,例如一個HTML文件。它首先通過傳遞上一級目錄的路徑和子路徑給File累的構造方法來實例化java.io.File類。

File file = new File(HttpServer.WEB_ROOT, request.getUri());

??? 然后它檢查該文件是否存在。假如存在的話,通過傳遞File對象讓sendStaticResource構造一個java.io.FileInputStream對象。然后,它調用FileInputStreamread方法并把字節數組寫入OutputStream對象。請注意,這種情況下,靜態資源是作為原始數據發送給瀏覽器的。

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);
???? }
}

??? 假如文件并不存在,sendStaticResource方法發送一個錯誤信息到瀏覽器。

String errorMessage =
???? "Content-Type: text/html\r\n" +
???? "Content-Length: 23\r\n" +
???? "\r\n" +
???? "<h1>File Not Found</h1>";
output.write(errorMessage.getBytes());

轉載于:https://www.cnblogs.com/macula7/archive/2009/08/16/1960791.html

總結

以上是生活随笔為你收集整理的Tomcat源码学习(7)-How Tomcat works(转)的全部內容,希望文章能夠幫你解決所遇到的問題。

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