下面我们将主要介绍如何实现一个基于java的Http服务器

 

Http服务器主要由三个类构成:HttpServerRequestResponse。其中程序的入口在HttpServer类,它调用await()方法,使得Server开始等候客户端的连接。当客户端连接后,它将把静态的页面内容发送给客户端浏览器。下面分别介绍这三个类:

1HttpServer

 HttpServer需要有一个服务器的根目录这在WEB_ROOT变量中定义的:public static final String WEB_ROOT =System.getProperty("user.dir") + File.separator  + "webroot";当我们运行服务器的时候可以通过-D选项指定环境变量user.dir的值。这个类中最重要的方法就是await()方法,内容如下: 

 1 public void await() { 
 2     ServerSocket serverSocket = null 3     int port = 8080 4     try { 
 5       serverSocket =  new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1")); 
 6     } 
 7     catch (IOException e) { 
 8       e.printStackTrace(); 
 9       System.exit(1); 
10     } 
11     // Loop waiting for a request 
12     while (!shutdown) { 
13       Socket socket = null14       InputStream input = null15       OutputStream output = null16       try { 
17         socket = serverSocket.accept(); 
18         input = socket.getInputStream(); 
19         output = socket.getOutputStream(); 
20 
21         // create Request object and parse 
22         Request request = new Request(input); 
23         request.parse(); 
24 
25         // create Response object 
26         Response response = new Response(output); 
27         response.setRequest(request); 
28         response.sendStaticResource(); 
29 
30         // Close the socket 
31         socket.close(); 
32 
33         //check if the previous URI is a shutdown command 
34         shutdown = request.getUri().equals(SHUTDOWN_COMMAND); 
35       } 
36       catch (Exception e) { 
37         e.printStackTrace(); 
38         continue39       } 
40     } 
41   } 

    await()方法内构造一个ServerSocket的实例,等客户端连接进来的时候把socket.getInputStream()传递给Request类进行解析,把socket.getOutputStream()传递给Response类,然后再把request对象传递给Response,最后调用Response.sendStaticResource()方法发送数据给客户端。socket.close()后监测是不是接受到了关闭Server的命令,如果是的话跳出循环结束程序。

 

 

2. Request

  Request类的主要目的是对http请求进行封装,它有一个InputStream类型参数的构造器 

 1 public Request(InputStream input) {  2     this.input = input;  3   }  

 

  同时它还有一个重要的String类型的成员变量uriRequest的目的就是从inputStream中提取uri,这是由两个函数实现的

 1 public void parse() { 
 2     // Read a set of characters from the socket 
 3     StringBuffer request = new StringBuffer(2048); 
 4     int i; 
 5     byte[] buffer = new byte[2048]; 
 6     try { 
 7       i = input.read(buffer); 
 8     } 
 9     catch (IOException e) { 
10       e.printStackTrace(); 
11       i = -112     } 
13     for (int j=0; j<i; j++) { 
14       request.append((char) buffer[j]); 
15     } 
16     System.out.print(request.toString()); 
17     uri = parseUri(request.toString()); 
18   } 
19   private String parseUri(String requestString) { 
20     int index1, index2; 
21     index1 = requestString.indexOf(' '); 
22     if (index1 != -1) { 
23       index2 = requestString.indexOf(' ', index1 + 1); 
24       if (index2 > index1) 
25         return requestString.substring(index1 + 1, index2); 
26     } 
27     return null28   } 

  其中parseUri(String request)方法是私有方法,它把String参数进行解析把两个空格之间的字符串返回,这是遵循Http请求的定义规则的,如果你不清楚可以参考基于java实现http服务器之一。

 

3.Response

 Response的两个重要的成员变量分别是OutputStream类型的outputRequeset类型的request,这个类的功能就是从Request的实例里面得到uri,然后和WEB_ROOT进行相加得到文件所在的绝对路径,然后读取这个文件的内容把它写入到socket.getOutputStream()里面去。

 1 public void sendStaticResource() throws IOException { 
 2     byte[] bytes = new byte[BUFFER_SIZE]; 
 3     FileInputStream fis = null 4     try { 
 5       File file = new File(HttpServer.WEB_ROOT, request.getUri()); 
 6       if (file.exists()) { 
 7         fis = new FileInputStream(file); 
 8         int ch = fis.read(bytes, 0, BUFFER_SIZE); 
 9         while (ch!=-1) { 
10           output.write(bytes, 0, ch); 
11           ch = fis.read(bytes, 0, BUFFER_SIZE); 
12         } 
13       } 
14       else { 
15         // file not found 
16         String errorMessage = "HTTP/1.1 404 File Not Found/r/n" + 
17           "Content-Type: text/html/r/n" + 
18           "Content-Length: 23/r/n" + 
19           "/r/n" + 
20           "<h1>File Not Found</h1>"21         output.write(errorMessage.getBytes()); 
22       } 
23     } 
24     catch (Exception e) { 
25       // thrown if cannot instantiate a File object 
26       System.out.println(e.toString() ); 
27     } 
28     finally { 
29       if (fis!=null30         fis.close(); 
31     } 
32   } 
posted @ 2020-12-30 21:39  划水De雁小明  阅读(135)  评论(0编辑  收藏  举报