JDK6 provides a simple HTTP server API, whereby we can build our own embedded HTTP server, which supports HTTP and HTTPS protocols, and provides a partial implementation of the HTTP1.1, which can be implemented by extending an existing HTTP Server API to implement, the programmer must implement the HttpHandler interface on its own, Httpserver will invoke the callback method of the HttpHandler implementation class to handle the client request, where we call an HTTP request and its response as an exchange, Packaged as a Httpexchange class, Httpserver is responsible for passing httpexchange to the HttpHandler implementation class's callback method. The following code demonstrates how to create your own HTTP Server
/**
* 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);//Set Httpserver port to 8888
Hs.createcontext ("/chinajash", New MyHandler ())//request to/chinajash within the MyHandler class
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 = "T.sendresponseheaders (Response.length ());
OutputStream OS = T.getresponsebody ();
Os.write (Response.getbytes ());
Os.close ();
}
}