Tuesday, January 27, 2015

Simple example of Java HttpServer

com.sun.net.httpserver.HttpServer  implements a simple HTTP server. A HttpServer is bound to an IP address and port number and listens for incoming TCP connections from clients on this address.


It's a simple example of using HttpServer.
package java_httpserver;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class Java_HttpServer {

    public static void main(String[] args) {
        try {
            HttpServer httpServer = HttpServer.create(new InetSocketAddress(8000), 0);
            httpServer.createContext("/", new MyHttpHandler());
            httpServer.setExecutor(null);
            httpServer.start();
        } catch (IOException ex) {
            Logger.getLogger(Java_HttpServer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    static class MyHttpHandler implements HttpHandler{

        @Override
        public void handle(HttpExchange he) throws IOException {
            int responseCode_OK = 200;
            String response = "Hello from java-buddy";
            he.sendResponseHeaders(responseCode_OK, response.length());
            
            OutputStream outputStream = he.getResponseBody();
            outputStream.write(response.getBytes());
            outputStream.close();
            
            //try-with-resources form
            /*
            try (OutputStream outputStream = he.getResponseBody()) {
                outputStream.write(response.getBytes());
            }
            */

        }
        
    }
    
}

To access the server, open a browser and visit: http://localhost:8000/



Next example show how to implement HttpServer to download image.

No comments:

Post a Comment