我已经使用Sun的轻量级HttpServer在线发现了一个简单的HttpServer.
基本上主要功能如下:
public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
//Create the context for the server.
server.createContext("/", new BaseHandler());
server.setExecutor(null); // creates a default executor
server.start();
}
Run Code Online (Sandbox Code Playgroud)
我已经实现了BaseHandler接口的方法来处理Http请求并返回响应.
static class BaseHandler implements HttpHandler {
//Handler method
public void handle(HttpExchange t) throws IOException {
//Implementation of http request processing
//Read the request, get the parameters and print them
//in the console, then build a response and send it back.
}
}
Run Code Online (Sandbox Code Playgroud)
我还创建了一个通过线程发送多个请求的客户端.每个线程将以下请求发送到服务器:
HTTP://本地主机:8000/[上下文] INT ="+线程ID …