I have a form for registration on my website. When submitting the form, a Java Servlet is called. Within the Servlet I check my database if the username or email is already used. I want the Servlet to respond if the registration was completed or not and I want my client to receive this response and act accordingly. A simple result could be a text above the registration form saying "Registration successful" or "Email/username already in use". How can I let my client receive the response from the Servlet?
My form
<form id="registerForm" autocomplete="on">
  <h1>Registrera</h1>
  <p>
    <label for="usernamesignup" class="uname" data-icon="u">Användarnamn</label> 
    <input id="usernamesignup" name="usernamesignup" required="required" type="text"
    placeholder="användarnamn" />
  </p>
  <p>
    <label for="emailsignup" class="youmail" data-icon="e">E-mail</label> 
    <input id="emailsignup" name="emailsignup" required="required" type="email"
    placeholder="namn@mail.com" />
  </p>
  <p>
    <label for="passwordsignup" class="youpasswd" data-icon="p">Lösenord</label> 
    <input id="passwordsignup" name="passwordsignup" required="required" type="password"
    placeholder="lösenord" />
  </p>
  <p>
    <label for="passwordsignup_confirm" class="youpasswd" data-icon="p">Upprepa lösenord</label> 
    <input id="passwordsignup_confirm" name="passwordsignup_confirm" required="required" type="password"
    placeholder="lösenord" />
  </p>
  <p class="signin button">
    <input type="submit" value="Registrera"/>
  </p>
  <p class="change_link">
    Redan medlem? <a href="#tologin" class="to_register">Logga in</a>
  </p>
</form>
My 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!
        }
        ...
    }
}
My solution
I went for a solution where I send redirects from my Servlet and append a status parameter. Core JSTL then retrieves this parameter and display a message accordingly. I do the following in my 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 my JSP I do:
<div id="registerMessage">
    <c:set var="status" value="${param.status}" />
    <c:if test="${status == '1'}">
        <p class="success">Du är nu registrerad och kan logga in!</p>
    </c:if>
    <c:if test="${status == '2'}">
        <p class="fail">Användarnamnet eller email adressen finns redan!</p>
    </c:if>
</div>
 
     
     
     
    