Add login submission from front-end
This commit is contained in:
@@ -22,24 +22,42 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
<header>
|
|
||||||
<h1>Welcome!</h1>
|
|
||||||
</header>
|
|
||||||
|
|
||||||
<main>
|
<main>
|
||||||
<input placeholder="Username">
|
<input id="username-field" placeholder="Username">
|
||||||
<br>
|
<br>
|
||||||
<input placeholder="Password">
|
<input id="password-field" placeholder="Password">
|
||||||
<br>
|
<br>
|
||||||
<button>Connect</button>
|
<button id="connect-button">Connect</button>
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
|
|
||||||
<footer>
|
|
||||||
</footer>
|
|
||||||
|
|
||||||
<script>
|
<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>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
@@ -8,12 +8,7 @@ import java.io.IOException;
|
|||||||
public class HtmlManager {
|
public class HtmlManager {
|
||||||
|
|
||||||
public String serveFile(String path){
|
public String serveFile(String path){
|
||||||
String content = readFile(path);
|
return 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String readFile(String path){
|
private String readFile(String path){
|
||||||
|
|||||||
@@ -1,35 +1,55 @@
|
|||||||
package httpsServer.httpServer.src;
|
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.html.HtmlManager;
|
||||||
import common.common.src.socket.SocketManager;
|
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.*;
|
import java.net.InetSocketAddress;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
public class Main {
|
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();
|
final HtmlManager htmlManager = new HtmlManager();
|
||||||
int port = 8043;
|
HttpServer server = HttpServer.create(new InetSocketAddress(port), 5);
|
||||||
try (ServerSocket serverSocket = new ServerSocket(port)) {
|
|
||||||
System.out.println("Server started on port " + port);
|
|
||||||
|
|
||||||
while (true) {
|
System.out.println("Server started on port " + port);
|
||||||
try (Socket clientSocket = serverSocket.accept()) {
|
|
||||||
BufferedReader in = new BufferedReader(
|
|
||||||
new InputStreamReader(clientSocket.getInputStream()));
|
|
||||||
|
|
||||||
String requestLine = in.readLine();
|
server.createContext("/", exchange -> {
|
||||||
System.out.println("Request: " + requestLine);
|
String content = htmlManager.serveFile("./assets/pages/index.html");
|
||||||
|
byte[] data = content.getBytes(StandardCharsets.UTF_8);
|
||||||
|
|
||||||
String response = htmlManager.serveFile("./assets/pages/index.html");
|
exchange.getResponseHeaders().add("Content-Type", "text/html; charset=UTF-8");
|
||||||
SocketManager.send(clientSocket, response);
|
exchange.sendResponseHeaders(200, data.length);
|
||||||
|
send(exchange, data);
|
||||||
|
});
|
||||||
|
|
||||||
} catch (IOException e) {
|
server.createContext("/login", exchange -> {
|
||||||
System.err.println("Client connection error: " + e.getMessage());
|
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