anotherCha

This commit is contained in:
2024-09-21 12:03:25 -04:00
parent f384067ac0
commit a66ffc8910
9 changed files with 90 additions and 0 deletions
+33
View File
@@ -0,0 +1,33 @@
import java.io.OutputStream;
import java.net.InetSocketAddress;
import com.sun.net.httpserver.HttpServer;
public class Application {
public static void main(String[] args) throws Exception {
int serverPort = 8084;
// Create an HTTP server and bind it to the specified port
HttpServer server = HttpServer.create(new InetSocketAddress(serverPort), 0);
// Create a context for the "/api/hello" endpoint
server.createContext("/api/hello", (exchange -> {
String respText = "Hello world"; // The response text
// Send the response headers (HTTP 200 OK, content length of the response)
exchange.sendResponseHeaders(200, respText.getBytes().length);
// Get the response body output stream and write the response to it
OutputStream outputStream = exchange.getResponseBody();
outputStream.write(respText.getBytes());
outputStream.flush();
// Close the exchange to finish the request
exchange.close();
}));
server.setExecutor(null); // Use default executor
server.start(); // Start the server
System.out.println("Server started on port " + serverPort);
}
}