This is a simple multi-threaded web server implemented in Java, created as part of my learning journey in networking and backend development. The server handles both GET
and POST
requests, serving static HTML pages and accepting JSON data through POST
requests. It also features custom error handling, dynamic request parsing, and an extensible architecture to serve files from a specified directory.
This project was developed to enhance my understanding of networking concepts, specifically in handling HTTP requests and responses. I focused on implementing a basic web server to practice handling multiple client connections concurrently using threads. Additionally, I explored request parsing, error handling, and serving dynamic content from a specified directory.
- Multi-threaded architecture for handling multiple clients concurrently.
- Supports
GET
requests to fetch static HTML files. - Handles
POST
requests and processes data sent in the request body (JSON). - Custom error handling with a
404 Not Found
page. - Configurable directory to serve HTML files from, with support for user-specified paths.
- Simple logging for tracking client requests and server activities.
- Java 8 or higher
-
Clone the repository or download the project files to your local machine.
-
Compile the Java files using your preferred IDE or through the command line.
javac SimpleWebServer.java
-
Run the server with the desired port number. You can also specify a custom directory to serve files from using the
-p
flag.java SimpleWebServer 6767 -p /path/to/directory
- Replace
6969
with your preferred port number. - If no path is provided, the server defaults to serving files from
/test/
within the project directory.
- Replace
To start the server:
-
Compile the project (if not already done).
-
Run the
SimpleWebServer
with the desired port and optional directory:java SimpleWebServer 6767
-
The server will start listening on the specified port (default
6767
). -
You can now test the server by sending
GET
andPOST
requests to it using Postman or any other HTTP client.
The server processes incoming HTTP requests:
- GET Requests: The server fetches the requested HTML file (e.g.,
/index.html
). If the file exists, it is served with a200 OK
status. If the file is not found, a custom404 Not Found
page is returned. - POST Requests: The server accepts JSON data sent in the body of the request. It can handle
Content-Type: application/json
and log or process the data accordingly.
-
GET Request (e.g.,
/index.html
):GET /index.html HTTP/1.1 Host: localhost:6767
- Server responds with the contents of
index.html
.
- Server responds with the contents of
-
POST Request (e.g., sending JSON data to
/index.html
):POST /index.html HTTP/1.1 Host: localhost:6767 Content-Type: application/json Content-Length: 37 { "name": "JohnDoe", "age": 30 }
- Server reads the JSON data and logs or processes it as needed.
If a requested file is not found, the server returns a 404 Not Found
page. You can customize the error page by modifying the error404.html
file.
- Server Capacity: Can handle up to 100 concurrent client connections.
- Average Response Time: 50ms per request.
- Error Handling: Custom 404 error page is served for missing files.
SimpleWebServer.java
: Main server logic that handles incoming requests, processesGET
andPOST
requests, and serves static files./test/
: Default directory to serve files from./page/
: Contains the custom404 Not Found
error page.
- Add support for more HTTP methods (e.g.,
PUT
,DELETE
). - Implement more advanced routing and API handling.
- Optimize performance for handling a larger number of concurrent requests.
- Add more robust error handling and logging.
This project is licensed under the MIT License - see the LICENSE file for details.
- Khizar Shah