Add login submission from front-end

This commit is contained in:
2025-12-03 16:34:53 +01:00
parent da011dbdcd
commit 63a9c13941
3 changed files with 69 additions and 36 deletions

View File

@@ -22,24 +22,42 @@
</head>
<body>
<header>
<h1>Welcome!</h1>
</header>
<main>
<input placeholder="Username">
<input id="username-field" placeholder="Username">
<br>
<input placeholder="Password">
<input id="password-field" placeholder="Password">
<br>
<button>Connect</button>
<button id="connect-button">Connect</button>
</main>
<footer>
</footer>
<script>
let btn = document.getElementById("connect-button");
btn.addEventListener("click", () => {
let username = document.getElementById("username-field").value;
let pwd = document.getElementById("password-field").value;
let data = {
username: username,
password: pwd
};
fetch("/login", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(data)
})
.then(response => response.text())
.then(result => {
console.log("Server response:", result);
})
.catch(error => {
console.error("Error:", error);
});
});
</script>
</body>
</html>

View File

@@ -8,12 +8,7 @@ import java.io.IOException;
public class HtmlManager {
public String serveFile(String path){
String content = readFile(path);
return "HTTP/1.1 200 OK\r\n" +
"Content-Type: text/html\r\n" +
"Content-Length: " + content.length() + "\r\n" +
"\r\n" +
content;
return readFile(path);
}
private String readFile(String path){

View File

@@ -1,35 +1,55 @@
package httpsServer.httpServer.src;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpServer;
import common.common.src.html.HtmlManager;
import common.common.src.socket.SocketManager;
import java.io.*;
import java.net.*;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
public class Main {
public static void main(String[] args) {
public static void main(String[] args) throws IOException {
final int port = 8043;
final HtmlManager htmlManager = new HtmlManager();
int port = 8043;
try (ServerSocket serverSocket = new ServerSocket(port)) {
HttpServer server = HttpServer.create(new InetSocketAddress(port), 5);
System.out.println("Server started on port " + port);
while (true) {
try (Socket clientSocket = serverSocket.accept()) {
BufferedReader in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
server.createContext("/", exchange -> {
String content = htmlManager.serveFile("./assets/pages/index.html");
byte[] data = content.getBytes(StandardCharsets.UTF_8);
String requestLine = in.readLine();
System.out.println("Request: " + requestLine);
exchange.getResponseHeaders().add("Content-Type", "text/html; charset=UTF-8");
exchange.sendResponseHeaders(200, data.length);
send(exchange, data);
});
String response = htmlManager.serveFile("./assets/pages/index.html");
SocketManager.send(clientSocket, response);
server.createContext("/login", exchange -> {
if (!"POST".equalsIgnoreCase(exchange.getRequestMethod())) {
exchange.sendResponseHeaders(405, -1);
return;
}
} catch (IOException e) {
System.err.println("Client connection error: " + e.getMessage());
}
}
} catch (IOException e) {
e.printStackTrace();
InputStream is = exchange.getRequestBody();
String body = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))
.lines()
.reduce("", (acc, line) -> acc + line + "\n");
System.out.println("Received POST data:\n" + body);
String response = "Data received!";
exchange.sendResponseHeaders(200, response.getBytes().length);
send(exchange, response.getBytes());
});
server.start();
}
private static void send(HttpExchange exchange, byte[] data) throws IOException {
try (OutputStream os = exchange.getResponseBody()) {
os.write(data);
}
}
}