I just finished a Java application I have been working on for a while. Only thing left I want to do is to implement registration online. Since I'm used to Java, I created my own HTTPServer (not using any API, I like to re-inventing the wheel) which handle requests and so on. It works fine.
However I'm very new to HTML, and I have searched A LOT on the internet, but can't figure out how to do what I want:
I have a SQLite database on my server which I want the webpage to register users in.
I implemented this using the HTML code below. It sends a http POST message to my server with the input from the user, this works fine (see Java code below).
My problem is that I can't figure out how to respond when the user tries to register an already registered user in the database. What I want to do is show a simple message and tell the user to try an other username.
Would very much appreciate if someone could fill in what I am missing.
HTML index.html
<!DOCTYPE html>
<html>
<body>
<form name=”Login” action =”success.html” method="post" target="_blank">
Username: <input type="text" name="name"><br>
Password: <input type=”password” name=”pass”><br>
<input type="submit" value="Submit">
</form>
<p>Register here.</p>
</body>
</html>
Java code:
//Code from http post received.
String[] input = last.split("=|\\&");
String loginName = input[1];
SQLServer sqlServer = new SQLServer();
if (sqlServer.containsLogin(loginName)) {
System.out.println("Registrated: " + loginName + "already in databse");
//Respond somehow to the client that user already in database.
} else {
sqlServer.register(loginName, input[3]);
System.out.println("Registrated: " + loginName + "with password: " + input[3]);
}
Edit: new try after help from Palpatim:
output.println("HTTP/1.0 401 Unauthorized");
output.println("Server: Java HTTP Server 1.0");
output.println("Date: " + new Date());
output.println("Content-type: " + type);
output.println("Content-length: " + fileLength);
output.println(); //blank line between headers and content
output.flush(); //flush character output stream buffer
//After that code I send the file.
Is that header formed the right way? Because it doesn't seem to work.
The file is the html file Palpatim posted. Also is it possible to set the action in the for to nothing? So it won't do anything until I either send OK or 401 from the server?