This is a http server program which can be used with another program that requires a (simple) server implementation.
Feel free to give any feedback.
Main.java file can be referred for example.
Step 1 (Optional):
Create an object of ServerProperties
class. All the properties are optional. The KeyStoreFileName and KeyStorePassword are required for HTTPS server.
ServerProperties serverProperties = new ServerProperties();
serverProperties.setPort(8080);
serverProperties.setHost("127.0.0.1");
serverProperties.setKeyStoreFileName("/path/to/file.jks");
serverProperties.setKeyStorePassword("password");
serverProperties.setServerTimeoutInMillis(10000);
serverProperties.setServerName("Test Server");
serverProperties.setRootDirectory("");
serverProperties.setLogFileName("log_file.txt");
serverProperties.setLogDateFormat("[HH:mm:ss]");
Step 2:
To start the server, create an instance of the HttpServer
or HttpsServer
class and assign it to Server class. Optionally, the serverProperties
can be passed.
Server httpServer = new HttpServer();
Server httpServer = new HttpServer(serverProperties);
Server httpsServer = new HttpsServer();
Server httpsServer = new HttpsServer(serverProperties);
Step 3: Add the controllers to the server.
// create response object with 404 not found error and default response body
httpServer.addController("GET", "/index.html", request -> new Response(404));
// create response object with 200 status code and "This is index page" as the response body
httpServer.addController("GET", "/index.html", request -> {
// process the request
return new Response().setBody("This is index page");
});
Step 4:
Call the start()
method to start the server.
httpServer.start();
Step 5:
Call the stop()
method to stop the server.
httpServer.stop();