Here the httpserver, not where the class library is specifically downloaded, but in JDK1.6, in the Com.sun.net.httpserver package, provides a simple higher level of the HTTP SERVERAPI, you can build the built-in HTTP Server, which supports HTTP and HTTPS protocols, provides a partial implementation of the HTTP1.1, and the part that is not implemented can be implemented by extending an existing HTTP Server API. The programmer must implement the HttpHandler interface on its own, and 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, wrapped into a httpexchange class, Httpserver is responsible for passing httpexchange to the HttpHandler implementation class's callback method. (It's an official description).
Using a simple example below, you can see how to use them, and detailed functionality to view the API documentation. Visit localhost:8086/and localhost:8086/test to see
Package Com.test.myjava;
Import java.io.IOException;
Import Java.io.OutputStream;
Import java.net.InetSocketAddress;
Import Java.util.Queue;
Import java.util.concurrent.*;
Import Com.sun.net.httpserver.HttpExchange;
Import Com.sun.net.httpserver.HttpHandler;
Import Com.sun.net.httpserver.HttpServer;
public class Httpservertest {public static void main (string[] args) {try {//Allow maximum number of connections int backLog = 10;
Inetsocketaddress Inetsock = new inetsocketaddress (8086);
Httpserver httpserver = httpserver.create (Inetsock, BackLog);
Return directly to Hello ... httpserver.createcontext ("/", New Handlertesta ());
Displays the number of requests that have been processed, using the thread pool Httpserver.createcontext ("/test", New Handlertestb ());
Httpserver.setexecutor (NULL);
Httpserver.start ();
System.out.println ("Httpserver Test start!");
catch (Exception e) {e.printstacktrace (); Direct processing Request Class Handlertesta implements httphandler{public void handle (Httpexchange httpexchange) throws Ioexcep tion {//TODOAuto-generated the HTTP protocol String responsestring = "<font color= ' #ff0000 ' >he when responding to requests for processing parts//returns requested by the method stub// Llo!
This is a httpserver!</font> ";
Set Response Head httpexchange.sendresponseheaders (Responsestring.length ());
OutputStream OS = Httpexchange.getresponsebody ();
Os.write (Responsestring.getbytes ());
Os.close ();
}//thread pool will not be used, abbreviated use, meaning a little gap, behind the analysis class HANDLERTESTB implements httphandler{private static int requestnum = 0;
Threadpoolexecutor Threadpoolexecutor; Handlertestb () {///two regular threads, max 3 threadpoolexecutor = new Threadpoolexecutor (2,3, timeunit.seconds, new
Arrayblockingqueue<runnable> (2), New Threadpoolexecutor.callerrunspolicy ()); The public void handle (Httpexchange He) throws IOException {//TODO auto-generated Method stub if ((Getqueuesize (thread
Poolexecutor.getqueue ()) <2) {requesttasks RQT = new Requesttasks (he);
Threadpoolexecutor.execute (RQT); else System.out.println ("please wait!");
Private synchronized int getqueuesize (queue queue) {return queue.size ();
}///processing requested task class Requesttasks implements runnable{static int processednum = 0;
Httpexchange Httpexchange;
Requesttasks (Httpexchange He) {httpexchange = he;
processednum++;
public void Run () {//TODO auto-generated Method Stub System.out.println ("Processednum:" +processednum);
String responsestring = "Processednum:" + processednum + "\ n";
try{httpexchange.sendresponseheaders (Responsestring.length ());
OutputStream OS = Httpexchange.getresponsebody ();
Os.write (Responsestring.getbytes ());
Os.close ();
Remove the annotation to see only two responses, some problems//while (true);
}catch (Exception e) {e.printstacktrace (); }
}
}
Look at these when there is a small episode, at that time do not understand since there is a createcontext () method to implement the HttpHandler interface of the class to process the request, why have a setexecutor to set the management thread of the executor, But also before the start () method, when you look at the document when you see Createcontext (), the Start () method is an abstract method (of course, Httpserver is also abstract class), is how to call it. Find its source on the web, only to find that the creation of Httpserver is first created through the static method of Create (inetsocketaddress addr, int backlog), which is through Httpserverprovider (It is also an abstract class, whose subclasses Sun.net.httpserver.DefaultHttpServerProvider in the Createhttpserver) to create, and then, in the concrete creation process, a new instance of the Httpserverimpl class (this is just a wrapper, the implementation is a Serverimpl class to complete the final Httpserver object generation); see the implementation of the Serverimpl. It is known that Setexecutor sets the thread that handles the TCP link request, while the Createcontext sets the callback method for handling the specific request, and can be invoked multiple times by setting Createcontext (). Set different paths using different or identical processing methods. Why around so much, adopt what design pattern, I am not to that altitude, expect someone to give a message to explain, or oneself later slowly experience.
Then put these things together, think about a general application scenario.
Reference
Three ways to implement HTTP server http://lpn520.iteye.com/blog/781273