Java HTML / XML How to - Create Simple Server for Hosting XML








Question

We would like to know how to create Simple Server for Hosting XML.

Answer

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
/* w  ww. j  a  v a  2  s .c o m*/
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;

public class Main {
    public static void main(String[] args) throws IOException {
        HttpServer server = HttpServer.create(new InetSocketAddress(8888), 0);
        server.createContext("/foo", new HttpHandler() {
            public void handle(HttpExchange t) throws IOException {
                t.sendResponseHeaders(200, 0);
                OutputStream out = t.getResponseBody();
                out.write("hello world".getBytes());
                out.close();
            }
        });
        server.start();
    }
}