1. 程式人生 > >java實現一個簡單的Web伺服器

java實現一個簡單的Web伺服器

Web伺服器也稱為超文字傳輸協議伺服器,使用http與其客戶端進行通訊,基於java的web伺服器會使用兩個重要的類,

java.net.Socket類和java.net.ServerSocket類,並基於傳送http訊息進行通訊。

這個簡單的Web伺服器會有以下三個類:

*HttpServer

*Request

*Response

應用程式的入口在HttpServer類中,main()方法建立一個HttpServer例項,然後呼叫其await()方法,顧名思義,await()方法會在指定埠

上等待HTTP請求,對其進行處理,然後傳送響應資訊回客戶端,在接收到關閉命令前,它會保持等待狀態。

該應用程式僅傳送位於指定目錄的靜態資源的請求,如html檔案和影象,它也可以將傳入到的http請求位元組流顯示到控制檯,但是,它並不傳送

任何頭資訊到瀏覽器,如日期或者cookies等。

下面為這幾個類的原始碼:

Request:

package cn.com.server;

import java.io.InputStream;

public class Request {
	private InputStream input;
	
	private String uri;
	
	public Request(InputStream input){
		this.input=input;
	}
	
	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 (Exception 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());
	}
	
	public 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;
	}
	
	public String getUri(){
		return this.uri;
	}
}
Request類表示一個HTTP請求,可以傳遞InputStream物件來建立Request物件,可以呼叫InputStream物件中的read()方法來讀取HTTP請求

的原始資料。

上述原始碼中的parse()方法用於解析Http請求的原始資料,parse()方法會呼叫私有方法parseUrI()來解析HTTP請求的URI,除此之外,並沒有

做太多的工作,parseUri()方法將URI儲存在變數uri中,呼叫公共方法getUri()會返回請求的uri。

Response:

<span style="font-size:10px;">package cn.com.server;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 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, BUFFER_SIZE);
					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) {
			System.out.println(e.toString());
		}finally{
			if(fis!=null){
				fis.close();
			}
		}
	}
}</span><span style="font-size:24px;">
</span>
Response物件在HttpServer類的await()方法中通過傳入套接字中獲取的OutputStream來建立。

Response類有兩個公共方法:setRequest()和sendStaticResource(),setRequest()方法會接收一個Request物件為引數,sendStaticResource()

方法用於傳送一個靜態資源到瀏覽器,如Html檔案。

HttpServer:

package cn.com.server;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

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 invoke.
	 */
	public static final String WEB_ROOT=System.getProperty("user.dir")+File.separator+"webroot";
	
	private static final String SHUTDOWN_COMMAND="/SHUTDOWN";
	
	private boolean shutdown=false;
	
	public static void main(String[] args) {
		HttpServer server=new HttpServer();
		server.await();
	}
	
	public void await(){
		ServerSocket serverSocket=null;
		int port=8080;
		try {
			serverSocket=new ServerSocket(port,1,InetAddress.getByName("127.0.0.1"));
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(0);
		}
		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();
			} catch (Exception e) {
				e.printStackTrace();
				continue;
			}
		}
	}
}
這個類表示一個Web伺服器,這個Web伺服器可以處理對指定目錄的靜態資源的請求,該目錄包括由公有靜態變數final WEB_ROOT指明的目錄及其所有子目錄。

現在在webroot中建立一個html頁面,命名為index.html,原始碼如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>Hello World!</h1>
</body>
</html>

現在啟動該WEB伺服器,並請求index.html靜態頁面。

對應的控制檯的輸出:

如此,一個簡單的http伺服器便完成了。