954,574 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

username availability using ajax

pls help me tell whats wrong
here is the code of my files
i am a total newbie......pls
t.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" language="javascript" src="ah.js"></script>
</head>
<body>
<table>
<tr>
        <td width="36%">Userid j</td>
        <td width="33%">
            <form name="f1" >
          <input type="text" name="user"  onBlur="startRequest()"/>
          
          
          <input type="button" name="btn" value="click" onClick="startRequest()" /></form>
          
        </td>
        <td width="31%" id="results">&nbsp;</td>
    </tr>
</table>

</body>
</html>


ah.js

var xmlHttp;// global instance of XMLHttpRequest
function createXmlHttpRequest()
{
       if(window.ActiveXObject)
       {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }

    else if(window.XMLHttpRequest)
    {
        xmlHttp=new XMLHttpRequest();
        
     }

}

function startRequest()
{

  createXmlHttpRequest();
alert("hi4");
  var u1=document.f1.user.value;
  alert("hi");
  xmlHttp.open("GET","http://localhost:8080/ajax/Jas?user="+u1 ,true)
  alert("hi2");
  xmlHttp.onreadystatechange=handleStateChange();
  alert("hi3");
  xmlHttp.send(null);

}

function handleStateChange()
{alert("handle" );

    if(xmlHttp.status==200)
    { alert("ready");
        if(xmlHttp.readyState==4)
            {alert("status ok");
          var message =  xmlHttp.responseXML.getElementsByTagName("valid")[0].childNodes[0].nodeValue;

             document.getElementById("results").innerHTML=message;
            }
        else
        {
           alert("Error loading pagen"+ xmlHttp.status +
":"+xmlHttp.statusText);
        }
    }
}


UserOp.java

import java.sql.*;
import java.util.*;
public class UserOp {
public boolean containsKey(String a)
{
	String urlPrefix = "jdbc:db2://localhost:50000/";
	String url;
	//String user;
	String empNo; 
	Connection con;
	Statement stmt;
	ResultSet rs;
	ArrayList<String> al=new ArrayList<String>();
	System.out.println ("**** Enter class EzJava");


	url = urlPrefix + "PRAC";

	try
	{
	// Load the driver
	Class.forName("com.ibm.db2.jcc.DB2Driver");
	System.out.println("**** Loaded the JDBC driver");
	// Create the connection using the IBM Data Server Driver for JDBC and SQLJ
	con = DriverManager.getConnection (url,"db2inst1","1491");
	// Commit changes manually
	con.setAutoCommit(false);
	System.out.println("**** Created a JDBC connection to the data source");
	// Create the Statement
	stmt = con.createStatement();
	System.out.println("**** Created JDBC Statement object");
	// Execute a query and generate a ResultSet instance
	rs = stmt.executeQuery("SELECT * FROM USERS"); 
	System.out.println("**** Created JDBC ResultSet object");
	// Print all of the employee numbers to standard System.output device
	while (rs.next()) {
	empNo = rs.getString(2);
	al.add(empNo);
	System.out.println("Employee number = " + empNo);
	}
	System.out.println("**** Fetched all rows from JDBC ResultSet");
	// Close the ResultSet
	rs.close();
	System.out.println("**** Closed JDBC ResultSet");
	// Close the Statement
	stmt.close();
	System.out.println("**** Closed JDBC Statement");
	// Connection must be on a unit-of-work boundary to allow close
	con.commit();
	System.out.println ( "**** Transaction committed" );
	// Close the connection
	con.close();
	System.out.println("**** Disconnected from data source");
	System.out.println("**** JDBC Exit from class EzJava - no errors");
	}
	catch (ClassNotFoundException e)
	{
	System.out.println("Could not load JDBC driver");
	System.out.println("Exception: " + e);
	e.printStackTrace();
	}
	catch(SQLException ex) 
	{
	System.err.println("SQLException information");
	while(ex!=null) {
	System.out.println ("Error msg: " + ex.getMessage());
	System.out.println ("SQLSTATE: " + ex.getSQLState());
	System.out.println ("Error code: " + ex.getErrorCode());
	ex.printStackTrace();
	ex = ex.getNextException(); // For drivers that support chained exceptions

	}
	}
	String ia[]=new String[al.size()];
	ia=al.toArray(ia);
	int c=(ia.length);
	boolean ab=false;
	System.out.println("sup"+a);
	for(int i=0;i<c;i++)
	{
		if(ia[i].equals(a))
			{
			ab=true;
			System.out.println("up"+ia[i]);
			}
		else
			{
			System.out.println(ia[i]);
			}
	}
	if(ab==true)
	{
		return true;
	}
	else
	{
	return false;
	}
}
public static void main(String q[])
{
	UserOp a=new UserOp();
System.out.println((a.containsKey("jas1291")));
		
}
}


Jas.java

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class for Servlet: Jas
 *
 */
 public class Jas extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 1L;
   
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public Jas() {
		super();
	}   	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String targetId = request.getParameter("user");
		System.out.print(targetId);
	       //check the id. If it is not existing already then return true else false
		UserOp userOp=new UserOp();

		
	           if ((targetId != null) && !userOp.containsKey(targetId.trim())) {
	            response.setContentType("text/xml");
	            response.setHeader("Cache-Control", "no-cache");
	            response.getWriter().write("<valid>true</valid>");
	        } else {
	            response.setContentType("text/xml");
	            response.setHeader("Cache-Control", "no-cache");
	            response.getWriter().write("<valid>false</valid>");
	        }

	}  	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}   	  	    
}
jasleen12345
Newbie Poster
18 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

What errors do you get?

syrellNardo
Newbie Poster
8 posts since Feb 2012
Reputation Points: 6
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: