• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • 基于Jave的Web服務工作機制(6)

    發表于:2007-05-25來源:作者:點擊數: 標簽:
    parseUri 方法從請求行那里得到URI。Listing 1.3 展示了parseUri 方法的用途。 parseUri 減縮請求中的第一個和第二個空格來獲得URI。 Listing 1.3. The Request class' parseUri method private String parseUri(String requestString) { int index1, index2
     parseUri 方法從請求行那里得到URI。Listing 1.3 展示了parseUri 方法的用途。 parseUri 減縮請求中的第一個和第二個空格來獲得URI。

      Listing 1.3. The Request class' 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;
    }

      Response類

      Response表示一個HTTP響應。它的構造函數接受一個OutputStream對象,比如下面的:

      public Response(OutputStream output) {
      this.output = output;
      }
      Response 對象被HttpServer類的await方法構造,該方法被傳遞的參數是從socket那里得到的OutputStream對象。

      Response類有兩個公共方法: setRequest和sendStaticResource. setRequest方法傳遞一個Request對象給Response對象。Listing 1.4中的代碼顯示了這個:

      Listing 1.4. The Response class' setRequest method

      public void setRequest(Request request) {
      this.request = request;
      }
      sendStaticResource 方法用來發送一個靜態資源,比如HTML文件。Listing 1.5給出了它的實現過程:

      Listing 1.5. The Response class' sendStaticResource method

      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.kjueaiud.com

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>