JDK6.0的新特性:轻量级Http Server

         JDK6提供了一个简单的Http Server API,据此我们可以构建自己的嵌入式Http Server,它支持Http和Https协议,提供了HTTP1.1的部分实现,没有被实现的那部分可以通过扩展已有的Http Server API来实现,程序员必须自己实现HttpHandler接口,HttpServer会调用HttpHandler实现类的回调方法来处理客户端请求,在这里,我们把一个Http请求和它的响应称为一个交换,包装成HttpExchange类,HttpServer负责将HttpExchange传给HttpHandler实现类的回调方法.下面代码演示了怎样创建自己的Http Server
None.gif package  jdk6;
None.gif
None.gif
import  java.io.IOException;
None.gif
import  java.net.InetSocketAddress;
None.gif
None.gif
None.gif
import  com.sun.net.httpserver.HttpServer;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  HTTPServerAPITester  dot.gif {
InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
InBlock.gif     * The main method.
InBlock.gif     * 
InBlock.gif     * 
@param args the args
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void main(String[] args) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try dot.gif{
InBlock.gif            HttpServer hs 
= HttpServer.create(new InetSocketAddress(8888),0);//设置HttpServer的端口为8888
InBlock.gif
            hs.createContext("/soddabao"new MyHandler());//用MyHandler类内处理到/chinajash的请求
InBlock.gif
            hs.setExecutor(null); // creates a default executor
InBlock.gif
            hs.start();
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (IOException e) dot.gif{
InBlock.gif            e.printStackTrace();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif package  jdk6;
None.gif
None.gif
import  java.io.IOException;
None.gif
import  java.io.OutputStream;
None.gif
None.gif
import  com.sun.net.httpserver.HttpExchange;
None.gif
import  com.sun.net.httpserver.HttpHandler;
None.gif
None.gif
//  TODO: Auto-generated Javadoc
ExpandedBlockStart.gifContractedBlock.gif
/** */ /**
InBlock.gif * The Class MyHandler.
ExpandedBlockEnd.gif 
*/

ExpandedBlockStart.gifContractedBlock.gif
public   class  MyHandler  implements  HttpHandler  dot.gif {
InBlock.gif       
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//* (non-Javadoc)
InBlock.gif        * @see com.sun.net.httpserver.HttpHandler#handle(com.sun.net.httpserver.HttpExchange)
ExpandedSubBlockEnd.gif        
*/

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

ExpandedBlockEnd.gif    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值