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

首頁 > 開發 > 綜合 > 正文

基于Jave的Web服務工作機制(4)

2024-07-21 02:14:19
字體:
來源:轉載
供稿:網友
  • 網站運營seo文章大全
  • 提供全面的站長運營經驗及seo技術!
  • 在下面的段落中,讓我們來看看這三個類吧。

     

      httpserver 類

      httpserver類表示一個web服務器,且在公共靜態目錄web_root及它的子目錄中能為找到的那些靜態資源而服務。web_root用以下方式初始化:

      public static final string web_root =
      system.getproperty("user.dir") + file.separator + "webroot";

      這段代碼指明了一個包含靜態資源的webroot目錄,這些資源可用來測試該應用。在該目錄中也能找到servlet容器。

      要請求一個靜態資源,在瀏覽器中輸入如下地址或url:

      http://machinename:port/staticresource
      machinename 是運行這個應用的計算機名或者ip地址。如果你的瀏覽器是在同一臺機器上,可以使用localhost作為機器名。端口是8080。staticresource是請求的文件夾名,它必須位于web-root目錄中。

      必然,如果你使用同一個計算機來測試應用,你想向httpserver請求發送一個index.html 文件,那么使用如下url:

      http://localhost:8080/index.html

      想要停止服務器,可以通過發送一個shutdown命令。該命令是被httpserver 類中的靜態shutdown_command變量所定義:

      private static final string shutdown_command = "/shutdown";
      因此,要停止服務,你可以使用命令:

      http://localhost:8080/shutdown
      現在讓我們來看看前面提到的await方法。下面一個程序清單給出了解釋。

      listing 1.1. the httpserver class' await method

      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實例而開始的。然后它進入了一個while循環:

      serversocket = new serversocket(
      port, 1, inetaddress.getbyname("127.0.0.1"));

      ...

      // loop waiting for a request
      while (!shutdown) {
        ...
      }

      socket = serversocket.accept();
      在收到一個請求后,await方法從accept方法返回的socket實例中獲得java.io.inputstream 和java.io.outputstream對象。

    發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 昌江| 永康市| 那坡县| 平舆县| 镇原县| 绥芬河市| 三台县| 高台县| 榆中县| 玉环县| 台北县| 土默特左旗| 遵义市| 紫云| 仙居县| 介休市| 宁国市| 彩票| 九龙城区| 辽阳县| 花莲县| 离岛区| 惠水县| 永川市| 清河县| 富民县| 乌拉特后旗| 汨罗市| 富平县| 阿坝| 庆阳市| 松江区| 晋宁县| 伽师县| 历史| 金昌市| 长治县| 鲜城| 洪湖市| 山东| 磴口县|