国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

基于Jave的Web服務工作機制6

2024-09-05 00:17:06
字體:
來源:轉載
供稿:網(wǎng)友

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響應。它的構造函數(shù)接受一個OutputStream對象,比如下面的:

  public Response(OutputStream output) {
  this.output = output;
  }
  Response 對象被HttpServer類的await方法構造,該方法被傳遞的參數(shù)是從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 方法用來發(fā)送一個靜態(tài)資源,比如HTML文件。Listing 1.5給出了它的實現(xiàn)過程:

  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"
        "

File Not Found

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 河津市| 荣成市| 江津市| 平昌县| 镇平县| 海晏县| 乌拉特后旗| 长宁区| 巫溪县| 上林县| 巴楚县| 江孜县| 灌云县| 江西省| 高雄市| 昌平区| 柯坪县| 成都市| 彰化市| 鱼台县| 永康市| 城固县| 蚌埠市| 镇雄县| 葫芦岛市| 西林县| 东乡族自治县| 红安县| 吉首市| 通海县| 綦江县| 丹江口市| 修文县| 安阳市| 土默特左旗| 日土县| 新宾| 芮城县| 锡林浩特市| 大荔县| 彰化市|