冷面閻羅

          低調(diào)做人&&高調(diào)做事
          隨筆 - 208, 文章 - 3, 評(píng)論 - 593, 引用 - 0
          數(shù)據(jù)加載中……

          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-10 19:59 冷面閻羅 閱讀(3310) 評(píng)論(1)  編輯  收藏 所屬分類: java

          評(píng)論

          # re: JDK6.0的新特性:輕量級(jí)Http Server  回復(fù)  更多評(píng)論   

          能決輸出中文內(nèi)容的問(wèn)題嗎!!!

          ···············
          2007-08-23 17:04 | shish
          主站蜘蛛池模板: 米易县| 太白县| 元朗区| 汽车| 南漳县| 区。| 奉新县| 工布江达县| 灵宝市| 清流县| 七台河市| 文安县| 澄迈县| 黔江区| 怀来县| 海口市| 虞城县| 中超| 桐庐县| 雷州市| 皮山县| 仙桃市| 奇台县| 正镶白旗| 新泰市| 正阳县| 奉贤区| 龙口市| 南平市| 桃江县| 明溪县| 新昌县| 太原市| 柏乡县| 会宁县| 绥德县| 古浪县| 延津县| 阿瓦提县| 河源市| 柳林县|