New features of JDK6.0: Lightweight HTTP Server

Source: Internet
Author: User

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 ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.