A multithreaded web server in Java that handles multiple simultaneous connections using a fixed thread pool. It uses a custom microframework to simplify the development of RESTful services and static file management. The server responds to HTTP GET and POST requests, serves static files from a predefined directory, and handles custom RESTful services. It includes robust error handling and supports basic HTTP response codes. It is ideal for learning about concurrent programming, microframework design, and basic web server functionality in Java.
SimpleWebServer is a straightforward Java-based web server implementation that handles both HTTP GET and POST requests. The server serves static files and supports asynchronous communication with REST services.
- GET and POST Request Handling: Supports HTTP GET requests to serve static files and POST requests to receive and display data.
- Static File Support: Serves HTML, CSS, JS, and image files from a specific directory.
- Asynchronous Communication with REST Services: Performs asynchronous GET requests to external REST services and handles responses efficiently.
-
SimpleWebServer.java:
- Main class that initializes the web server.
- Sets up a thread pool to handle multiple concurrent connections.
- Listens on the specified port and delegates request handling to
ClientHandler.
-
ClientHandler.java:
- Implements the
Runnableinterface to manage client communication. - Processes GET and POST requests, serving static files or responding to data sent by the client.
- Handles asynchronous communication with REST services via the
RestClientclass.
- Implements the
-
RestClient.java:
- Client for making asynchronous HTTP GET requests.
- Uses
HttpClientto send requests and handle responses asynchronously.
-
Service.java:
- Description:
The
Serviceinterface provides a contract for services that process requests and generate responses. Implementations of this interface should define how to handle requests and generate corresponding responses. - Methods:
getValue(String request, String response): Processes the given request and response, and returns a string value based on the processing.
- Usage: This interface is intended to be implemented by classes that define specific services. For example, a service could process an HTTP GET request and return the result as a string.
- Description:
The
-
WebServer.java:
- Description:
The
WebServerclass is a simple framework for managing web services and static files. It allows developers to define RESTful services using lambda expressions, extract query parameters from HTTP requests, and specify the location of static files to be served by the server. - Attributes:
services: AConcurrentHashMapthat stores the mapping of URL paths toServiceinstances, ensuring thread-safe access.staticFilesLocation: AStringthat stores the path to the directory containing static files.
- Methods:
get(String url, Service s): Registers a new service to a specific URL path.queryParams(String request, String param): Extracts the value of a specific query parameter from a given HTTP request.staticfiles(String location): Sets the location of the static files directory.
- Usage: This class acts as the backbone of the web server, allowing for easy registration of services and management of static files. It's designed to simplify the development of web services by abstracting common tasks like request handling and query parameter extraction.
- Description:
The
-
RestClientTest.java:
- Unit tests for the
RestClientclass. - Verifies the behavior of the
sendAsyncGetRequestmethod for successful and failed HTTP requests. - Uses Mockito to mock
HttpClientresponses and test the functionality ofRestClient.
- Unit tests for the
-
SimpleWebServerTest.java:
- Unit tests for the
SimpleWebServerclass. - Verifies that the server can start correctly and accept connections.
- Runs the server in a separate thread to avoid blocking test execution and checks the connection to port 8080.
- Unit tests for the
-
User Interaction:
- Users send HTTP requests to the server, which processes and responds according to the request type (GET or POST).
-
Request Handling:
- The server uses a thread pool to handle multiple simultaneous connections.
- GET requests serve static files or data from REST services.
- POST requests receive and display data sent by the client.
-
Asynchronous Communication:
- The
RestClientclass handles asynchronous GET requests to external services and manages responses.
- The
- Java Development Kit (JDK) 17 or higher
-
Clone the Repository:
git clone https://github.com/JohannBulls/SimpleWebServer.git cd SimpleWebServer -
Compile the Project:
mvn clean install
-
Run the Server:
mvn exec:java
-
Access the Application:
- Open your web browser and go to
http://localhost:8080. - You can access static files, perform POST requests, and view the received data.
- Open your web browser and go to
-
File examples:
- These are the example files that appear for the execution of the program.
To run unit tests, ensure you have JUnit and Mockito set up in your project. Then, execute the following commands:
- Java: Primary programming language.
- HttpClient: For asynchronous communication with REST services.
- ExecutorService: For handling multiple concurrent requests.
- JUnit: For unit testing.
- Mockito: For mocking objects in unit tests.
- Johann Amaya Lopez - JohannBulls - GitHub
This project is licensed under the MIT License. See the LICENSE file for details.



