A cool and simple HTTP Server implementation using pure Java
HTTPUsingJava.mp4
This project is a simple yet robust HTTP server using Java to handle basic HTTP requests and serve predefined routes. The server supports multithreading, allowing it to handle multiple client connections simultaneously. It's an excellent project for understanding the basics of HTTP, server programming, and multithreading in Java.
- Multithreaded Handling: Supports simultaneous client connections.
- Default Routes:
/
: A welcoming page with the current date and time./time
: Returns the current time in JSON format./health
: Reports the server's health status./stats
: Displays basic server statistics like active threads and available memory.
- Customizable Routes: Easily add new routes with specific handlers.
- HTML and JSON Responses: Supports serving both HTML and JSON responses.
- Error Handling: Handles invalid routes with a
404 Not Found
response.
- Java Development Kit (JDK): Version 8 or higher.
- terminal or CLI interface
git clone https://github.com/ProgrammerPratik/HTTP-server-implementation.git
cd SimpleHttpServer
javac SimpleHttpServer.java
java SimpleHttpServer
just visit:
http://localhost:8080/
OR
http://127.0.0.1:8080/
You can also check the data sent by the user on the server side. This includes the headers and other details from the HTTP request such as User-Agent, Accept, and more as shown below
The server provides a simple interface to add custom routes. Here’s an example:
server.addRoute("/hello", (request) ->
server.createHtmlResponse("<h1>Hello, World!</h1>")
);
After adding this code, the route /hello
will serve a basic HTML response.
Request: curl http://localhost:8080/time
Response:
{"time": "2024-11-19T19:15:45.9178453"}
Request: curl http://localhost:8080/health
Response:
{"status": "healthy", "uptime": "1732023983004"}
For any issues or questions, please contact me here.