This is a simple HTTP server implemented in Java that serves HTML and PHP files. It can handle both GET and POST requests and execute PHP scripts.
Before running the server, ensure you have the following:
- Java Development Kit (JDK) installed.
- PHP installed and added to your system's PATH (for PHP script execution).
- Or else the php directory of your own wamp/mamp/xampp server should be given instead of php in line
141
of the code.
ProcessBuilder processBuilder = new ProcessBuilder("php", filePath, params);
-
Clone or download this repository to your local machine.
-
Open a terminal or command prompt and navigate to the project directory.
-
Compile the
HttpServer.java
file:
javac HttpServer.java
- Run the server:
java HttpServer
- The server will start listening on port
2728
. You can access it in a web browser.http://localhost:2728/
- HttpServer.java: The main Java server program.
- add.php: A sample PHP script that performs addition based on POST or GET parameters.
- index.html: A simple HTML form for submitting numbers to the add.php script.
- Handles GET and POST requests.
- Serves static HTML files from the htdocs directory.
- Executes PHP scripts and passes parameters.
You can modify the server's behavior by editing the HttpServer.java file. For example, you can change the port number, the root directory for HTML files (htdocs), or add more handling logic for different HTTP methods or file types. Any other resources that you wish to include can be added to the htdocs folder as well.
This project is licensed under the GNU License - see the LICENSE file for details.
This project was created as a simple example of a Java-based HTTP server with PHP support. It can be used as a starting point for more complex web server applications or educational purposes.