最代码官方的gravatar头像
最代码官方 2014-11-28 11:43:05

分享java最简单web server代码,只支持一个http get请求

package com.zuidaima.simpleserver;

import java.io.IOException;
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;

/*
 * a simple static http server
 * @author www.zuidaima.com
 */
public class SimpleHttpServer {

	public static void main(String[] args) throws Exception {
		HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
		server.createContext("/zuidaima", new MyHandler());
		server.setExecutor(null); // creates a default executor
		server.start();
	}

	static class MyHandler implements HttpHandler {
		public void handle(HttpExchange t) throws IOException {
			String response = "www.zuidaima.com";
			t.sendResponseHeaders(200, response.length());
			OutputStream os = t.getResponseBody();
			os.write(response.getBytes());
			os.close();
		}
	}

}

访问:http://localhost:8000/zuidaima

分享java最简单web server代码,只支持一个http get请求


打赏

已有2人打赏

农村落魄小青年的gravatar头像 jqdunye的gravatar头像

最代码最近下载分享源代码列表最近下载
最代码最近浏览分享源代码列表最近浏览
tartaglia  LV2 4月16日
annazhang  LV29 2023年10月3日
2036495585  LV9 2023年9月26日
卢本伟不开挂  LV4 2023年9月17日
cheung524071  LV8 2023年8月23日
sdhfkh  LV4 2023年6月12日
张三asdcsv 2023年6月6日
暂无贡献等级
微信网友_6411724627349504  LV3 2023年4月3日
tdfgjbi0de  LV6 2023年2月24日
a1246775443 2023年2月4日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友