Live Availability Checking with Java.
Wall Script
Wall Script
Sunday, February 14, 2010

Live Availability Checking with Java.

Some days I had posted an article about User name live availability checking with PHP and jquery. In this post my brother Ravi Tamada explained the same with Java technologies like JSP and servelts using MySQL database.

Live Availability Checking with Java.


Download Script     Live Demo

About Author
Ravi Tamada
Computer science engineering, Final Year
India, Twitter Profile @ravi8x
( ravi8x[at]gmail.com )

You just download the script copy and paste the directory into Tomcat Webapps directory
Database
MySQL users table columns uid, uname, passcode.
CREATE TABLE users
(
uid INT PRIMARY KEY AUTO_INCREMENT,
uname VARCHAR(50) UNIQUE,
passcode VARCHAR(50)
);

index.jsp
$('#uname').change(function(){} - uname is the ID of the input. Using $("#uname").val() calling input field value. First checking the value string length max 3 (uname.length > 3)
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$(".uname").change(function()
{
var uname = $(this).val();
if(uname.length > 3)
{
$(".status").html("Checking availability...");
$.ajax
({
type: "POST",
url: "check",
data: "uname="+ uname,
success: function(msg)
{
$(".status").ajaxComplete(function(event, request, settings)
{
$(".status").html(msg);
});
}
});
}

else
{
$(".status").html("username shold be 3 chars");
}

});
});

</script>
<div>
<label>user name :</label>
<input type="text" class="uname"/><span class="status"></span>
</div>

check.java
Contains java code. Download link Mysql Java Connector Jar file
import java.io.*;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;

public class check extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try
{
// 9lessons is my database name
String connectionURL = "jdbc:mysql://localhost:3306/9lessons";
Connection connection = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root", "root");
String uname = request.getParameter("uname");
PreparedStatement ps = connection.prepareStatement("select uname from USERS where uname=?");
ps.setString(1,uname);
ResultSet rs = ps.executeQuery();

if (!rs.next())
{
out.println("<b>"+uname+"</b> is avaliable");
}
else
{
out.println("<font color=red><b>"+uname+"</b> is already in use</font>");
}
out.println();
}
catch (Exception ex)
{
out.println("Error ->" + ex.getMessage());
}
finally
{
out.close();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doPost(request, response);
}
}
web notification

18 comments:

  1. Very useful site !!
    Please include Google custom search in ur website.
    (google.com/webelements )
    difficult to use blogger's default search for finding things.

    ReplyDelete
  2. how can i prevent my page from doing the registration if the user use the username already use in the username field? tks

    ReplyDelete
  3. this is nice. i was looking for something like this. however i need help about some part of it.

    i use xampp for apache and mysql for my php system. i have a module in java that i need to connect. the java should go to that same database to retrieve values. how is can be achieved?

    also, can you provide some links to help me learn further about the mysql java connector and tomcat.

    thanks
    Yudz

    ReplyDelete
  4. Excelent code Ravi!

    one question this works very well on FireFox and Opera but not in IE, when you type any letter and hit enter key notghing appears!, any clues ??

    Steven

    ReplyDelete
  5. @steve

    Hello Steve thanx for ur comment. In this code i write code for invoking an event only when we change the content in the TEXT FIELD, this is done when we come out from the TEXT FIELD not when hitting ENTER key. This works in firefox when hitting ENTER key, this may be fire fox feature n in opera and IE hitting enter is not working.

    ReplyDelete
  6. this is an interesting script...i never thought there is a way in java to do this...thank you ravi

    ReplyDelete
  7. is this script well secured .i hope its not prone to hackers.

    ReplyDelete
  8. i want to know if its possible to insert an image button where you have it available

    ReplyDelete
  9. Nice...but can't get it to work :-(
    Copied all files into myapps
    Engineers explain things so other engineers can understand :-)

    Great I'm sure !
    thx

    ReplyDelete
  10. nice article but not work completely.when the error displayed the cursor not moved to the next field...
    Best luck

    ReplyDelete
  11. please help me... its urgent... the code is working fine.. but its posting the form even if the username already existed... i want to stop this please help me how should i do this.....

    ReplyDelete
  12. please help me. it urgent .this code is working but i need to disable the submit button when the user i already exist?

    ReplyDelete
  13. it's not working m getting error..
    n.extend.ajax jquery.min.js:19
    (anonymous function) register1.jsp:19
    n.event.handle jquery.min.js:19
    (anonymous function)

    ReplyDelete
  14. if get valid username then only navigate to next textbox otherwise onfocus in usename textbox how to validate in the example of (Live Availability Checking with Java). any one tell me

    ReplyDelete
  15. Hey this code is working but,its unable to return status from servlet to ajax...can any one tell me why this happening?

    ReplyDelete

mailxengine Youtueb channel
Make in India
X