Checking User Name Availability With AJAX – Google Style

Ajax » on Oct 25, 2011 { 38 Comments } By Sivateja

We will see how to check the user name availability in the database with ajax,  this is some thing what we can find out at time of gmail signup

Files Required

  • index.html
  • pass.jsp
  • web.xml


<script type="text/javascript">
function loadXMLDoc()
var xmlhttp;
var k=document.getElementById("id2").value;
var urls="pass.jsp?ver="+k;

if (window.XMLHttpRequest)
  xmlhttp=new XMLHttpRequest();
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  if (xmlhttp.readyState==4)


Enter name: <input type="text" name="id2" id="id2" onkeyup="loadXMLDoc()">
<span id="err"> </span>



<%@ page import="*,java.sql.*" %>
<%@ page contentType="text/html" pageEncoding="UTF-8"%>


            String sn=request.getParameter("ver");

                    Connection con =DriverManager.getConnection("","system","admin");
                    Statement st=con.createStatement();
                    //ResultSet rs = st.executeQuery("select * from emp where empno="+sn);
                    ResultSet rs = st.executeQuery("select * from emp where empname='"+sn+"'");  // this is for name
                        out.println("<font color=red>");
                        out.println("Name already taken");

                    }else {

                        out.println("<font color=green>");





<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="2.5">



Note: you must have classes12.jar, ojdbc14.jar in the lib folder of you application


Check Output

Click here to check the output


You Might Also Like

  ::. About the Author .::

Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.


38 Responses to “Checking User Name Availability With AJAX – Google Style”
  1. Sumit Kumar says:

    I tried this project at home but was unable to run. So, can you provide the download link for this project.


  2. Kavya says:

    Nicely explained.. Thank you

  3. smurfiiee says:

    Thank you!! it work !! (:

  4. Java4s says:

    @Kavya, @smurfiiee

    Thanks for your feedback 🙂

    @Sumit Kumar
    We can provide direct download link, if so you can’t get whats going exactly with ajax, just try once from your end slowly.

  5. Raju says:

    sir nice tutorial,but i couldn’t get something in the index.html file var urls=”pass.jsp?ver=”+k; and there is no attribute in in field,how can we use String sn=request.getParameter(“ver”); in pass.jsp.please explain it,thank you.

  6. Java4s says:


    See the flow…

    Once we enter any character at line number 34(index.html), onkeyup we will be moved to line number 4 right,
    the character we entered will be copied to variable k(line number 7), and we formed request url at line number 8 by passing k value(so k value will move along with URL hope you know this).

    In pass.jsp i have retrieved that k value by calling the attribute ‘ver’ because in the url we assigned k value to ‘ver’ variable.

    If you are fresher you may feel little confusion, but its very simple. Read it slowly.

  7. Vijay says:

    instead of using out.println is there any other way to send response from server ?

  8. amazing exaple……..thank u..

  9. thanks a lot dude..!!!!nice explained

  10. sefwetet says:

    nice tutorial….thanks a lot

  11. cool thanks sir.. working good

  12. pallavi says:

    Hi Siva,

    Thanks for wonderful answers and articles posted by you ..

    But whenever I am opening this site takes alot of time to load.firstly i thought it would be problem of my browser.But with whatever browser i open this site it is slow.Please could you make changes to this site such that we could be able to access this faster.

    Thanks once again!!!!!!!!!

  13. Nice SIr………..Good Job….Thank yOu

  14. anusha says:

    thank you sir,we need more examples,Please provide

  15. Abhinav says:

    what is the use of ” web.xml ” file?

  16. phphunger says:

    very nice tutorial.the same thing i have implemented in php…

  17. pavan says:

    Nice tutorial. Explanation was clear and simple. Thank you.

  18. Chandan says:

    Can you please show the code of ajax server side validation for checking userid already exists or not….by using struts and hibernate

  19. Fakhrul says:

    Very very nice explain sir thank you so much

  20. mahesh says:

    This code for using mysql database

    String sn = request.getParameter(“ver”);
    Connection connection= null;

    String url = “jdbc:mysql://localhost:3306/emp”;
    String userName = “root”;
    String password = “”;
    connection = DriverManager.getConnection(url, userName,

  21. Uday says:

    Thanks siva:)
    with the help of your explanations, I regained the hope to do programming!

  22. Sudhir says:

    What if i wanaa use Servlet instead of jsp?

  23. Mostafijur rahman says:

    thank you soooo much sir..great explanation.its working!! 😀 🙂

  24. mm says:

    its showing message only but its accepting even for already exists name in my program

  25. surendra kumar says:

    it is working fine but it display only first row in result to display all row in database

  26. Mukesh says:

    Beneficial Topic

  27. Subhankar Chowrasia says:

    Thank u for the code..

  28. akhildas says:

    good example excellent

  29. Mohsin says:

    You cant simply call the db from.jsp .This is not a correct approach i guess.

  30. Sivateja says:


    You are correct, but I have shown just to make it as a simple example.

  31. yogesh says:

    how to get it from dao without pass.jsp??

  32. Alok gautam says:

    it is not full code..i need to stop submit form..if user already exist…. please provide me

  33. srikanth says:

    SEVERE: Servlet.service() for servlet [jsp] in context with path [/uservalid] threw exception [java.lang.ClassNotFoundException: org.apache.jsp.userValid_jsp] with root cause

    can you tel me?

    why it should occur?

  34. makhna says:

    i have query . you have send the data to server by url like this “pass.jsp?ver=”+k and for that you have used get method .but i want to use post method .and is their any other way of sending ver to the server .please help me it will be really helpfull

  35. Yogesh says:

    Most importantly Your code compile without any error..Thanks For sharing Different modules…

  36. Vishnu Yarnaga says:



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.