知道美河 | 上传资料 | VIP申请 | 精品课程 | 资料搜索 | 问题反馈 | 会员手册 | 积分消费 | 积分充值 | 帐号保护
美河学习学习在线赞助VIP

美河学习在线(主站) eimhe.com

 找回密码
 建立账号
查看: 5399|回复: 3

[原创] jdk6新特性——http server

[复制链接]
发表于 2008-11-27 07:06:28 | 显示全部楼层 |阅读模式
启动下面代码后,浏览器中访问:http://127.0.0.1:8888/hujun
就可以看到结果啦,大家努力尝试其它吧

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;

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

public class MyHTTPServer {

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

}
}
}

class MyHandler implements HttpHandler {
public void handle(HttpExchange t) throws IOException {
InputStream is = t.getRequestBody();
String response = "<font color='#ff0000'>come on baby</font>";
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}

评分

1

查看全部评分

发表于 2008-11-27 10:24:24 | 显示全部楼层
发表于 2008-11-27 10:36:13 | 显示全部楼层
发表于 2009-4-3 17:32:44 | 显示全部楼层
您需要登录后才可以回帖 登录 | 建立账号

本版积分规则

 
QQ在线咨询

QQ|小黑屋|手机版|Archiver|美河学习在线 ( 浙网备33020302000026号 )

GMT+8, 2025-5-2 07:45

Powered by Discuz!

© 2001-2025 eimhe.com.

快速回复 返回顶部 返回列表