This project is a simple HTTP server implemented in Java. It was created as a learning exercise to understand the fundamentals of client-server architecture, HTTP protocols, and socket programming.
The server listens on port 80 and handles GET
requests from clients. It serves files from a predefined directory structure based on the requested resource path. The server also handles common HTTP errors such as 404 Not Found
and 405 Method Not Allowed
.
- Basic HTTP Server: Handles
GET
requests and serves static files. - Error Handling: Returns appropriate HTTP status codes for errors like
404 Not Found
and405 Method Not Allowed
. - Multi-threaded: Each client connection is handled in a separate thread, allowing the server to handle multiple clients concurrently.
- Dynamic Content-Type Detection: Uses
Files.probeContentType
to determine the MIME type of the requested resource.
- The server starts and listens on port 80.
- When a client connects, the server reads the HTTP request line and headers.
- The server processes the request:
- If the request method is not
GET
, it responds with a405 Method Not Allowed
error. - If the
Host
header is missing, it responds with a400 Bad Request
error. - If the requested resource does not exist, it responds with a
404 Not Found
error. - If the resource exists, it sends the file content with the appropriate
Content-Type
header.
- If the request method is not
- The server closes the connection after processing the request.
This project was created to achieve the following learning objectives:
- Understand the basics of client-server communication.
- Learn how to implement a simple HTTP server using Java sockets.
- Gain familiarity with HTTP protocols, headers, and status codes.
- Practice file handling and dynamic content-type detection in Java.
0.1.0
Copyright © 2025 Janinda Rukshan. All Rights Reserved.
This project is licensed under the MIT License
Thanks for checking it out!