Http Server in Mercury

Here’s the translation of the HTTP server example from Go to Java, with explanations in Markdown format suitable for Hugo:

Our HTTP server example demonstrates how to create a basic server using Java’s built-in com.sun.net.httpserver package.

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;

public class HttpServerExample {

    public static void main(String[] args) throws IOException {
        // Create an HTTP server that listens on port 8090
        HttpServer server = HttpServer.create(new InetSocketAddress(8090), 0);

        // Set up context handlers
        server.createContext("/hello", new HelloHandler());
        server.createContext("/headers", new HeadersHandler());

        // Start the server
        server.start();
        System.out.println("Server is listening on port 8090");
    }

    // A handler for the /hello endpoint
    static class HelloHandler implements HttpHandler {
        @Override
        public void handle(HttpExchange exchange) throws IOException {
            String response = "hello\n";
            exchange.sendResponseHeaders(200, response.length());
            try (OutputStream os = exchange.getResponseBody()) {
                os.write(response.getBytes());
            }
        }
    }

    // A handler for the /headers endpoint
    static class HeadersHandler implements HttpHandler {
        @Override
        public void handle(HttpExchange exchange) throws IOException {
            StringBuilder response = new StringBuilder();
            exchange.getRequestHeaders().forEach((key, values) -> {
                values.forEach(value -> response.append(key).append(": ").append(value).append("\n"));
            });
            exchange.sendResponseHeaders(200, response.length());
            try (OutputStream os = exchange.getResponseBody()) {
                os.write(response.toString().getBytes());
            }
        }
    }
}

In Java, we use the HttpServer class from the com.sun.net.httpserver package to create a simple HTTP server. This is analogous to using the net/http package in the original example.

The HttpHandler interface in Java is similar to the concept of handlers in the original example. We implement this interface to define how to handle incoming HTTP requests.

In the main method, we create an HttpServer instance, set up the context handlers for different endpoints, and start the server.

The HelloHandler class handles requests to the /hello endpoint. It sends a simple “hello” response.

The HeadersHandler class handles requests to the /headers endpoint. It reads all the HTTP request headers and echoes them in the response body.

To run the server:

  1. Save the code in a file named HttpServerExample.java.
  2. Compile the code:
$ javac HttpServerExample.java
  1. Run the compiled class:
$ java HttpServerExample

The server will start and listen on port 8090.

To test the server, you can use curl in another terminal:

$ curl localhost:8090/hello
hello

$ curl localhost:8090/headers
Host: localhost:8090
User-Agent: curl/7.68.0
Accept: */*

This example demonstrates how to create a basic HTTP server in Java, set up request handlers, and respond to different types of requests.