Registration forms on my website trigger a Java Servlet to check the database for existing usernames or emails. How can I ensure the client receives a response from the Servlet indicating whether the registration was successful or not? The desired outcome is to display a message above the registration form such as "Registration successful" or "Email/username already in use".
Registration Form
<form id="registerForm" autocomplete="on">
<h1>Register</h1>
<p>
<label for="usernamesignup" class="uname" data-icon="u">Username</label>
<input id="usernamesignup" name="usernamesignup" required="required" type="text"
placeholder="username" />
</p>
<p>
<label for="emailsignup" class="youmail" data-icon="e">E-mail</label>
<input id="emailsignup" name="emailsignup" required="required" type="email"
placeholder="email" />
</p>
<p>
<label for="passwordsignup" class="youpasswd" data-icon="p">Password</label>
<input id="passwordsignup" name="passwordsignup" required="required" type="password"
placeholder="password" />
</p>
<p>
<label for="passwordsignup_confirm" class="youpasswd" data-icon="p">Confirm Password</label>
<input id="passwordsignup_confirm" name="passwordsignup_confirm" required="required" type="password"
placeholder="confirm password" />
</p>
<p class="signin button">
<input type="submit" value="Register"/>
</p>
<p class="change_link">
Already a member? <a href="#tologin" class="to_register">Log In</a>
</p>
</form>
Java Servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("usernamesignup");
String email = request.getParameter("emailsignup");
String password = request.getParameter("passwordsignup");
try {
MysqlDataSource dataSource = new MysqlDataSource();
...
Connection conn = dataSource.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM Users WHERE username = '" + username + "' OR email = '" + email + "'");
if (!rs.next()) {
stmt.executeUpdate("INSERT INTO Users VALUES('" + username + "', '" + password + "', '" + email + "')");
//Notify the client that the registration was successful!
} else {
//Notify the client that the registration failed!
}
...
}
}
Proposed Solution
Redirects are sent from the Servlet with appended status parameters. This status parameter is then retrieved by Core JSTL in order to display a corresponding message. The following is implemented in the Servlet:
ResultSet rs = stmt.executeQuery("SELECT * FROM Users WHERE username = '" + username + "' OR email = '" + email + "'");
if (!rs.next()) {
stmt.executeUpdate("INSERT INTO Users VALUES('" + username + "', '" + password + "', '" + email + "')");
//Registration was successful!
response.sendRedirect ("pages/index.jsp?status=1");
} else {
//Registration failed
response.sendRedirect ("pages/index.jsp?status=2#toregister");
}
and in the JSP:
<div id="registerMessage">
<c:set var="status" value="${param.status}" />
<c:if test="${status == '1'}">
<p class="success">Registration successful! You can now log in.</p>
</c:if>
<c:if test="${status == '2'}">
<p class="fail">Username or email address already in use!</p>
</c:if>
</div>