Http Server in Miranda
Here’s the translated code and explanation in Markdown format suitable for Hugo:
Our first HTTP server program using Java. Here’s the full source code:
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 SimpleHttpServer {
public static void main(String[] args) throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress(8090), 0);
server.createContext("/hello", new HelloHandler());
server.createContext("/headers", new HeadersHandler());
server.setExecutor(null);
server.start();
}
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());
}
}
}
static class HeadersHandler implements HttpHandler {
@Override
public void handle(HttpExchange exchange) throws IOException {
StringBuilder response = new StringBuilder();
for (String key : exchange.getRequestHeaders().keySet()) {
response.append(key).append(": ")
.append(exchange.getRequestHeaders().getFirst(key))
.append("\n");
}
exchange.sendResponseHeaders(200, response.length());
try (OutputStream os = exchange.getResponseBody()) {
os.write(response.toString().getBytes());
}
}
}
}
Writing a basic HTTP server in Java involves using the com.sun.net.httpserver
package, which provides a simple HTTP server implementation.
A fundamental concept in Java HTTP servers is handlers. A handler is an object implementing the HttpHandler
interface. We define two handler classes: HelloHandler
and HeadersHandler
.
The HelloHandler
class serves a simple “hello” response:
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());
}
}
}
The HeadersHandler
class does something more sophisticated by reading all the HTTP request headers and echoing them into the response body:
static class HeadersHandler implements HttpHandler {
@Override
public void handle(HttpExchange exchange) throws IOException {
StringBuilder response = new StringBuilder();
for (String key : exchange.getRequestHeaders().keySet()) {
response.append(key).append(": ")
.append(exchange.getRequestHeaders().getFirst(key))
.append("\n");
}
exchange.sendResponseHeaders(200, response.length());
try (OutputStream os = exchange.getResponseBody()) {
os.write(response.toString().getBytes());
}
}
}
In the main
method, we create an HttpServer
instance, register our handlers on server routes using createContext
, and start the server:
public static void main(String[] args) throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress(8090), 0);
server.createContext("/hello", new HelloHandler());
server.createContext("/headers", new HeadersHandler());
server.setExecutor(null);
server.start();
}
To run the server:
$ javac SimpleHttpServer.java
$ java SimpleHttpServer &
Access the /hello
route:
$ curl localhost:8090/hello
hello
This example demonstrates how to create a basic HTTP server in Java, defining custom handlers for different routes, and processing HTTP requests and responses.