Choose Your Language

Thursday 16 May 2013

Checking User Name Availability Using AJAX, JSP And MySQL

userregistration.jsp


<%--
    Document   : userregistration
    Created on : 17 May, 2013, 11:22:42 AM
    Author     : Aravind Sankaran
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
var k=document.getElementById("username").value;
var urls="checkusername.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").innerHTML=xmlhttp.responseText;
     }
  }
xmlhttp.open("GET",urls,true);
xmlhttp.send();
}
</script>

    </head>
    <body>
        User Name: <input type="text" name="username" id="username" onkeyup="loadXMLDoc()">
        <span id="err"> </span>

    </body>
</html>


 checkusername.jsp


<%--
    Document   : checkusername
    Created on : 9 May, 2013, 12:42:25 PM
    Author     : Aravind Sankaran
--%>

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

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

                    Class.forName("com.mysql.jdbc.Driver");
                    Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/databasename","databaseusername","databasepassword");
                    Statement st=con.createStatement();
                    ResultSet rs = st.executeQuery("select * from table_name where username='"+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"?>
 
<welcome-file-list>
<welcome-file>userregistration.jsp</welcome-file>
</welcome-file-list>
</web-app>
 
 

7 comments:

  1. The code would be nice for those who want a simple solution. But security is relevant in this case!

    Never use "Statement st=con.createStatement();" and then unchecked string concatenation! That is a no go.
    Prepared statements are so much simpler to use and they are mostly safe.

    ReplyDelete
  2. it is very good
    thank you!

    ReplyDelete
  3. I want to make a validate() function so that the user cannot submit the form if he inserts an existing username

    form name="demo" action="RegisterServlet" method="get" onsubmit="return validate();"

    How should I proceed?

    ReplyDelete
  4. Please,if you have time take a look here

    http://pcgames4addicts.blogspot.ro/2014/04/i-am-uploading-file-on-server-and.html

    I am having trouble with a validate function

    ReplyDelete
  5. GOOD ATTEMPT AND A USEFUL ONE TOO REGARDLESS OF THE FEW GLITCHES THAT HAVE BEEN POINTED OUT EARLIER. BUT FOR HEAVEN'S SAKE DON'T USE SUCH BLINDING COLOR COMBINATIONS!
    TRY SIMPLER COLORS ON LIGHT BACKGROUND.

    ReplyDelete