/** * Created by IntelliJ IDEA. * User: Chinajash * Date: Dec 30, 2006 */ public class HTTPServerAPITester { public static void main(String[] args) { try { HttpServer hs = HttpServer.create(new InetSocketAddress(8888),0);//设置HttpServer的端口为8888 hs.createContext("/chinajash", new MyHandler());//用MyHandler类内处理到/chinajash的请求 hs.setExecutor(null); // creates a default executor hs.start(); } catch (IOException e) { e.printStackTrace(); } } } class MyHandler implements HttpHandler { public void handle(HttpExchange t) throws IOException { InputStream is = t.getRequestBody(); String response = "<h3>Happy New Year 2007!--Chinajash</h3>"; t.sendResponseHeaders(200, response.length()); OutputStream os = t.getResponseBody(); os.write(response.getBytes()); os.close(); } } |