在JAVA中,使用sun HttpServer实现一个web server的时候,使用了下面的hander类来处理请求:
static class MyHandler implements HttpHandler {
@Override
public void handle(HttpExchange httpExchange) throws IOException {
String response = "test"; // 返回固定内容
httpExchange.sendResponseHeaders(200, response.length());
OutputStream os = httpExchange.getResponseBody();
os.write(response.getBytes());
os.close();
}
}