Question

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?

Was it helpful?

Solution

On whatever socket pair you're using (assuming you didn't rewrite that as well), respond with the body of an HTTP response.

The full scope of the response is well outside the scope of a simple SO question, but the basics will be:

  • An HTTP status code (in your case, probably something in the 400 range) and message
  • Appropriate HTTP headers
  • An appropriate payload, probably consisting of either structured data or plain text, with the desired message.

For example, if you wanted the browser to display a page with a "Sorry, that user is already in the database" message, you could send this over the wire:

HTTP 401 Unauthorized
Content-Type: text/html

<html>
<head>
<title>Already in database</title>
<body>
<p>Sorry, that user is already in the database</p>
</body>
</html>

OTHER TIPS

Since you wrote your own httpserver it is hard to say how can you can get data from your data model to your view. In your case you might need to submit registration through an AJAX request and send your response back to the page as JSON then display the result message with javascript either by adding it to the DOM or with an alert().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top