Newsletter

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

Index.html

<html>
<head>
<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();
  }
else
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4)
    {
        //document.getElementById("err").style.color="red";
        document.getElementById("err").innerHTML=xmlhttp.responseText;

    }
  }
xmlhttp.open("GET",urls,true);
xmlhttp.send();
}
</script>
</head>
<body>

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

</body>
</html>

pass.jsp

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

<%

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

                    Class.forName("oracle.jdbc.driver.OracleDriver");
                    Connection con =DriverManager.getConnection("jdbc:oracle:thin:@www.java4s.com:1521:XE","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
                    if(rs.next())
                    {    
                        out.println("<font color=red>");
                        out.println("Name already taken");
                        out.println("</font>");

                    }else {

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

                    }

rs.close();
st.close();
con.close();

%>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

OutPut

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

Database

Check Output

Click here to check the output

​​

You Might Also Like

  ::. About the Author .::

Java4s_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.

Comments

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.

    Thanks.

  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:

    @Raju

    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 ..it 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;

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

  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 set.how 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:

    @Mohsin

    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:

    good

Name*
Mail*
Website



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.