I've searched for username availability in google. There was no any good solution to this problem. Here I'm trying to check username availability in database table user. I have following code
<input type="text" name="username" id="usernamee" tabindex="1"class="form-control" placeholder="Username"><span class="status">
this is my script code
<script>
$(function(){
$("#usernamee").blur(function(){
var uname = $('#usernamee').val();
if(uname.length >= 3){
$(".status").html("<font> Checking availability...</font>");
$.ajax({
type: "GET",
url: "/exist/"+uname,
success: function(msg){
$(".status").html(msg);
}
});
}
else{
$(".status").html("<font color=red>Username should be <b>3</b> character long.</font>");
}
});
});
</script>
this is my controller code
@RequestMapping(value = { "/exist/{name}" }, method = RequestMethod.POST)
public void checkUsername(@PathVariable("name") String username, HttpServletResponse response , HttpServletRequest request) throws IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String connectionURL = "jdbc:mysql://localhost:81/name"; // students is my database name
Connection connection = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = (Connection) DriverManager.getConnection(connectionURL, "root", "root");
PreparedStatement ps = (PreparedStatement) connection.prepareStatement("select username from users where username=?");
ps.setString(1,username);
ResultSet rs = ps.executeQuery();
if (!rs.next()) {
out.println("<font color=green><b>"+username+"</b> is avaliable");
}
else{
out.println("<font color=red><b>"+username+"</b> is already in use</font>");
}
out.println();
} catch (Exception ex) {
out.println("Error ->" + ex.getMessage());
} finally {
out.close();
}
}
When I run these code the statusspan tag only shows checking availability..... only. My controller haven't been invoked.
Above code is copied from internet. Any suggestions are welcomed.
Thank you in advance
errorcallback.