[Pratik Merekar]


PROJECT: HTTP Implementation using Java

19 Nov, 2024


Repository link: https://github.com/ProgrammerPratik/HTTP-server-implementation


Overview:

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.




features:


Requirements:

  1. Java Development Kit (JDK): Version 8 or higher.
  2. terminal or CLI interface

Usage:

1. Clone the Repository

git clone https://github.com/ProgrammerPratik/HTTP-server-implementation.git
cd SimpleHttpServer

2. Compile and run the Server

javac SimpleHttpServer.java
java SimpleHttpServer

3. Accessing the Server through your browser

just visit: http://localhost:8080/ OR http://127.0.0.1:8080/ for locally accessing the server you ran (by default the server will run on port 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:

cmd-image

How to add new Routes:

The server provides a simple interface to add custom routes. Here’s an example:

server.addRoute("/hello", (request) -> 
{
return new HttpResponse(200, "<h1>Hello, World!</h1>");
}

After adding this code, the route "/hello" will serve a basic HTML response.


Sample Curl outputs:

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"}

I also wrote an article on how you can implement simpler HTTP server using Java which explains how basics work incase you are curious.

Article link: https://programmerpratik.github.io/articles/http-using-java.html