Java Reference
In-Depth Information
tpHandler handler) method, where path specifies the root URI path,
and handler specifies the HttpHandler implementation that handles all
requests that target this path. If you prefer, you can invoke HttpContext
createContext(String path) without specifying an initial handler.
You would later specify the handler by calling HttpContext 's void
setHandler(HttpHandler h) method.
3. Starttheserver.Afteryouhavecreatedtheserverandatleastonecontext(in-
cludingasuitablehandler),thefinaltaskistostarttheserver.Accomplishthis
task by calling HttpServer 's void start() method.
I'vecreatedaminimalHTTPserverapplicationthatdemonstratesallthreetasks.This
application's source code appears in Listing 11-1 .
Listing 11-1. A minimal HTTP server application
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
class MinimalHTTPServer
{
public static void main(String[] args) throws IOExcep-
tion
{
HttpServer server = HttpServer.create(new InetSock-
etAddress(8000), 0);
server.createContext("/echo", new Handler());
server.start();
Search WWH ::




Custom Search