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 .:: | ||
Comments
38 Responses to “Checking User Name Availability With AJAX – Google Style”
I tried this project at home but was unable to run. So, can you provide the download link for this project.
Thanks.
Nicely explained.. Thank you
Thank you!! it work !! (:
@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.
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.
@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.
instead of using out.println is there any other way to send response from server ?
amazing exaple……..thank u..
thanks a lot dude..!!!!nice explained
nice tutorial….thanks a lot
cool thanks sir.. working good
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!!!!!!!!!
Nice SIr………..Good Job….Thank yOu
thank you sir,we need more examples,Please provide
what is the use of ” web.xml ” file?
very nice tutorial.the same thing i have implemented in php…
Nice tutorial. Explanation was clear and simple. Thank you.
Can you please show the code of ajax server side validation for checking userid already exists or not….by using struts and hibernate
Very very nice explain sir thank you so much
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);
Thanks, a lot, brother for your kind help
Thanks siva:)
with the help of your explanations, I regained the hope to do programming!
What if i wanaa use Servlet instead of jsp?
thank you soooo much sir..great explanation.its working!! 😀 🙂
its showing message only but its accepting even for already exists name in my program
yea same problem here also…its just showing already exists but if we try to insert it is inserting
it is working fine but it display only first row in result set.how to display all row in database
Beneficial Topic
Thank u for the code..
good example excellent
You cant simply call the db from.jsp .This is not a correct approach i guess.
@Mohsin
You are correct, but I have shown just to make it as a simple example.
how to get it from dao without pass.jsp??
it is not full code..i need to stop submit form..if user already exist…. please provide me
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?
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
Most importantly Your code compile without any error..Thanks For sharing Different modules…
good