love fish大鵬一曰同風(fēng)起,扶搖直上九萬(wàn)里

          常用鏈接

          統(tǒng)計(jì)

          積分與排名

          friends

          link

          最新評(píng)論

          JDK6.0的新特性:輕量級(jí)Http Server

          JDK6提供了一個(gè)簡(jiǎn)單的Http Server API,據(jù)此我們可以構(gòu)建自己的嵌入式Http Server,它支持Http和Https協(xié)議,提供了HTTP1.1的部分實(shí)現(xiàn),沒(méi)有被實(shí)現(xiàn)的那部分可以通過(guò)擴(kuò)展已有的Http Server API來(lái)實(shí)現(xiàn),程序員必須自己實(shí)現(xiàn)HttpHandler接口,HttpServer會(huì)調(diào)用HttpHandler實(shí)現(xiàn)類的回調(diào)方法來(lái)處理客戶端請(qǐng)求,在這里,我們把一個(gè)Http請(qǐng)求和它的響應(yīng)稱為一個(gè)交換,包裝成HttpExchange類,HttpServer負(fù)責(zé)將HttpExchange傳給HttpHandler實(shí)現(xiàn)類的回調(diào)方法.下面代碼演示了怎樣創(chuàng)建自己的Http Server
          package?jdk6;

          import?java.io.IOException;
          import?java.net.InetSocketAddress;


          import?com.sun.net.httpserver.HttpServer;

          public?class?HTTPServerAPITester?{
          ????
          ????
          /**
          ?????*?The?main?method.
          ?????*?
          ?????*?
          @param?args?the?args
          ?????
          */

          ????
          public?static?void?main(String[]?args)?{
          ????????
          try?{
          ????????????HttpServer?hs?
          =?HttpServer.create(new?InetSocketAddress(8888),0);//設(shè)置HttpServer的端口為8888
          ????????????hs.createContext("/soddabao",?new?MyHandler());//用MyHandler類內(nèi)處理到/chinajash的請(qǐng)求
          ????????????hs.setExecutor(null);?//?creates?a?default?executor
          ????????????hs.start();
          ????????}
          ?catch?(IOException?e)?{
          ????????????e.printStackTrace();
          ????????}

          ????}

          }

          package?jdk6;

          import?java.io.IOException;
          import?java.io.OutputStream;

          import?com.sun.net.httpserver.HttpExchange;
          import?com.sun.net.httpserver.HttpHandler;

          //?TODO:?Auto-generated?Javadoc
          /**
          ?*?The?Class?MyHandler.
          ?
          */

          public?class?MyHandler?implements?HttpHandler?{
          ???????
          ???????
          /*?(non-Javadoc)
          ????????*?@see?com.sun.net.httpserver.HttpHandler#handle(com.sun.net.httpserver.HttpExchange)
          ????????
          */

          ???????
          public?void?handle(HttpExchange?httpexchnge)?throws?IOException?{
          ??????????????httpexchnge.getRequestBody();
          ???????????String?response?
          =?"<h3>Happy?New?Year?2007!--Soddabao</h3>";
          ???????????httpexchnge.sendResponseHeaders(
          200,?response.length());
          ???????????OutputStream?os?
          =?httpexchnge.getResponseBody();
          ???????????os.write(response.getBytes());
          ???????????os.close();
          ???????}

          ????}

          posted on 2007-01-11 15:29 liaojiyong 閱讀(245) 評(píng)論(0)  編輯  收藏 所屬分類: Java

          主站蜘蛛池模板: 新营市| 永宁县| 玉树县| 柘荣县| 钟祥市| 铜鼓县| 密云县| 左贡县| 连南| 泗阳县| 田林县| 康定县| 囊谦县| 嘉义县| 建湖县| 信丰县| 疏勒县| 昭苏县| 理塘县| 贵州省| 汉源县| 塔城市| 射洪县| 天峨县| 瑞昌市| 买车| 安西县| 临高县| 霍邱县| 伽师县| 涟水县| 湟中县| 周宁县| 营山县| 辽中县| 洪雅县| 泽普县| 怀仁县| 平潭县| 博爱县| 勐海县|