This project is a lightweight multithreaded HTTP server implemented in Java. It serves as an educational tool to demonstrate key concepts of socket programming, networking, I/O handling, and concurrency.
The server can process basic HTTP requests, serve static files, and handle simple API routes, offering valuable insight into how HTTP communication works internally without using external frameworks.
The HTTP server listens for incoming TCP connections and handles each client in a separate thread using a thread pool. It supports both GET and POST methods, provides simple routing, and can serve files from a specified web root directory.
- Supports GET and POST requests.
- Serves static files from the
www/directory. - Built-in JSON API endpoint at
/api/hello. - Thread pool for concurrent client handling.
- Graceful startup and shutdown.
- MIME type detection for common file formats.
- Modular code design separating server, handler, request, and response logic.
| File | Description |
|---|---|
Main.java |
The entry point that initializes and starts the server. |
HttpServer.java |
Manages sockets, listens for clients, and assigns connections to threads. |
ClientHandler.java |
Handles individual client requests and generates appropriate responses. |
Request.java |
Parses incoming HTTP requests (method, headers, and body). |
Response.java |
Constructs and sends HTTP responses to clients. |
www/ |
The web root directory for static files. |
- Java 8 or higher
- Basic understanding of HTTP and Java I/O
-
Open a terminal and navigate to the project’s root directory.
-
Compile all Java files:
javac -d out src/*.java -
Run the server:
java -cp out Main 8080
Note: If no port number is specified, the server defaults to port
8080.
Once running, you can test the server using a web browser or command-line tools such as curl.
Visit: http://localhost:8080
curl http://localhost:8080/api/hello Expected response:
{"message": "Hello from your Java HTTP Server!"} Example 3: Send a POST Request Bash
curl -X POST -d "Hello Server" http://localhost:8080 Expected response:
{"received": "Hello Server"} Serving Static Files The server serves files from the www/ directory by default. For example, if www/index.html exists, it will be returned when you access the root path (/).
You can include HTML, CSS, JavaScript, or image files in this directory, and they will be served with the correct MIME type detection.
Author Author: Sadhvi Koli Date: November 2025 Purpose: Developed as a learning project for understanding Java networking and server-side programming.