Home > Java > javaTutorial > How Can I Build a Simple HTTP Server in Java Using jdk.httpserver?

How Can I Build a Simple HTTP Server in Java Using jdk.httpserver?

DDD
Release: 2024-12-08 00:38:11
Original
811 people have browsed it

How Can I Build a Simple HTTP Server in Java Using jdk.httpserver?

Simple HTTP Server in Java: A Comprehensive Guide

In the vast landscape of Java development, creating a simple HTTP server may seem like a tedious task involving manual parsing and formatting. However, with the introduction of Java SE 6, a built-in HTTP server has emerged, offering a simplified solution to this challenge.

Embedded HTTP Server in Java SE

Since Java SE 6, the jdk.httpserver module has provided a robust HTTP server functionality. This module encapsulates the core functionality for handling HTTP requests and formatting responses, eliminating the need for manual coding.

Implementation:

Here's a straightforward example of how to implement an HTTP server using the jdk.httpserver module:

// com.stackoverflow.q3732109
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 Test {

    public static void main(String[] args) throws Exception {
        HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
        server.createContext("/test", new MyHandler());
        server.setExecutor(null); // creates a default executor
        server.start();
    }

    static class MyHandler implements HttpHandler {
        @Override
        public void handle(HttpExchange t) throws IOException {
            String response = "This is the response";
            t.sendResponseHeaders(200, response.length());
            OutputStream os = t.getResponseBody();
            os.write(response.getBytes());
            os.close();
        }
    }
}
Copy after login

Execution and Response:

Execute this code and navigate to http://localhost:8000/test in your browser. The following response will be displayed:

This is the response
Copy after login

Note on com.sun.* Classes:

While it's generally recommended to avoid using com.sun.* classes, it's important to note that the HttpServer in jdk.httpserver is an exception. This class is guaranteed to be present in all Oracle JDKs, ensuring portability without compromising compatibility.

The above is the detailed content of How Can I Build a Simple HTTP Server in Java Using jdk.httpserver?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template