在上篇文章中我們介紹了Http協議的基本知識,以及Java中兩個重要的類Socket和SocketServer類,下面我們將主要介紹如何?一個基於java的Http伺服器。
Http伺服器主要由三個類構成:HttpServer、Request和Response。其中程式的入口在HttpServer類,它調用await()方法,使得Server開始等候用戶端的串連。當用戶端串連後,它將把靜態頁面內容發送給用戶端瀏覽器。下面分別介紹這三個類:
1:HttpServer類
HttpServer需要有一個伺服器的根目錄這在WEB_ROOT變數中定義的:
public static final String WEB_ROOT =System.getProperty("user.dir") + File.separator + "webroot";當我們運行伺服器的時候可以通過-D選項指定環境變數user.dir的值。這個類中最重要的方法就是await()方法,內容如下:
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;
}
}
}
await()方法內構造一個ServerSocket的執行個體,等用戶端串連進來的時候把socket.getInputStream()傳遞給Request類進行解析,把socket.getOutputStream()傳遞給Response類,然後再把request對象傳遞給Response,最後調用Response.sendStaticResource()方法發送資料給用戶端。socket.close()後監測是不是接受到了關閉Server的命令,如果是的話跳出迴圈結束程式。
2. Request類
Request類的主要目的是對http請求進行封裝,它有一個InputStream型別參數的構造器
public Request(InputStream input) {
this.input = input;
}
同時它還有一個重要的String類型的成員變數uri,Request的目的就是從inputStream中提取uri,這是由兩個函數實現的
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());
}
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;
}
其中parseUri(String request)方法是私人方法,它把String參數進行解析把兩個空格之間的字串返回,這是遵循Http請求的定義規則的,如果你不清楚可以參考基於java實現http伺服器之一。
2.Response類
Response的兩個重要的成員變數分別是OutputStream類型的output和Requeset類型的request,這個類的功能就是從Request的執行個體裡面得到uri,然後和WEB_ROOT進行相加得到檔案所在的絕對路徑,然後讀取這個檔案的內容把它寫入到socket.getOutputStream()裡面去。
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();
}
}
下面說明如何運行這個應用程式,首先你從下面的地址取得原始碼:http://www.onjava.com/onjava/2003/04/23/examples/HowWebServersWork.zip,把它解到你的D盤例如temp目錄下,它包括三個目錄src lib webroot其中webroot內有一個index.html檔案供測試用。從Command中運行
cd temp
javac -d . src/ex01/pyrmont/*.java
這樣在目前的目錄(d:/temp)會出現ex01/pyrmont目錄,裡面是編譯得到的class檔案,接下來運行
java -Duser.dir=d:/temp ex01.pyrmont.HttpServer
然後從瀏覽器輸入http://localhost:8080/index.html,這樣你就可以看到Server發送給你的靜態頁面index.html了。