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