Sample Java HTTP server demonstrating basic web server functionality using com.sun.net.httpserver.HttpServer
. This sample project showcases creating a lightweight HTTP server with multiple endpoint handlers.
- Java Development Kit (JDK) 8 or higher
- Basic understanding of Java and HTTP concepts
- Install Daytona: Follow the Daytona installation guide.
- Create the Workspace:
daytona create https://github.com/mhmohona/java-hello-world.git
- Compile the Java files:
javac HelloWorldServer.java
- Run the server:
java HelloWorldServer
- The server will start on
http://localhost:8080/
- Multiple Endpoint Handlers
- Root
/
handler with customizable message /api
endpoint supporting GET and POST requests- Graceful server shutdown handling
- Root
-
Root Endpoint (
/
)- Supports optional query parameter
message
- Default response: "Hello, World!"
- Example:
http://localhost:8080/?message=CustomGreeting
- Supports optional query parameter
-
API Endpoint (
/api
)- Supports GET and POST methods
- GET returns a simple JSON message
- POST echoes the request body in the response
- Uses
com.sun.net.httpserver.HttpServer
for HTTP handling - Implements thread pooling with
Executors.newFixedThreadPool()
- Includes error handling and logging
- Supports basic request routing
HelloWorldServer.java
: Main server implementation- Nested classes:
HelloWorldHandler
: Handles root endpointApiHandler
: Handles API endpoint requests
This project is open-source and available under the MIT License.