Add login submission from front-end
This commit is contained in:
@@ -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>
|
||||
@@ -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){
|
||||
|
||||
@@ -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)) {
|
||||
System.out.println("Server started on port " + port);
|
||||
HttpServer server = HttpServer.create(new InetSocketAddress(port), 5);
|
||||
|
||||
while (true) {
|
||||
try (Socket clientSocket = serverSocket.accept()) {
|
||||
BufferedReader in = new BufferedReader(
|
||||
new InputStreamReader(clientSocket.getInputStream()));
|
||||
System.out.println("Server started on port " + port);
|
||||
|
||||
String requestLine = in.readLine();
|
||||
System.out.println("Request: " + requestLine);
|
||||
server.createContext("/", exchange -> {
|
||||
String content = htmlManager.serveFile("./assets/pages/index.html");
|
||||
byte[] data = content.getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
String response = htmlManager.serveFile("./assets/pages/index.html");
|
||||
SocketManager.send(clientSocket, response);
|
||||
exchange.getResponseHeaders().add("Content-Type", "text/html; charset=UTF-8");
|
||||
exchange.sendResponseHeaders(200, data.length);
|
||||
send(exchange, data);
|
||||
});
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("Client connection error: " + e.getMessage());
|
||||
}
|
||||
server.createContext("/login", exchange -> {
|
||||
if (!"POST".equalsIgnoreCase(exchange.getRequestMethod())) {
|
||||
exchange.sendResponseHeaders(405, -1);
|
||||
return;
|
||||
}
|
||||
} 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user