Skip to content

A lightweight multithreaded HTTP server built in Java. Handles GET and POST requests, serves static files, and includes a simple API endpoint.

License

Notifications You must be signed in to change notification settings

sadhvikoli/java-http-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Java HTTP Server

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.


Overview

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.

Key Features

  • 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.

Project Structure

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.

How to Compile and Run

Requirements

  • Java 8 or higher
  • Basic understanding of HTTP and Java I/O

Steps

  1. Open a terminal and navigate to the project’s root directory.

  2. Compile all Java files:

    javac -d out src/*.java
  3. Run the server:

    java -cp out Main 8080

    Note: If no port number is specified, the server defaults to port 8080.


Testing the Server

Once running, you can test the server using a web browser or command-line tools such as curl.

Example 1: Access from a Browser

Visit: http://localhost:8080

Example 2: Test the API Endpoint

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.

About

A lightweight multithreaded HTTP server built in Java. Handles GET and POST requests, serves static files, and includes a simple API endpoint.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages