ajax認証ユーザー名


var xmlHttp;
	 
	function createXMLHttpRequest() {
		//         ie, ns,firefox
		if(window.XMLHttpRequest) {
			xmlHttp = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	
	function validate(field) {
//alert(document.getElementById("userId").value);
		if (trim(field.value).length != 0) {
			//  XMLHttpRequest
			createXMLHttpRequest();
			var url = "user_validate.jsp?userId=" + trim(field.value) + "&timestamp=" + new Date().getTime();
			xmlHttp.open("GET", url, true);
			//    ,         ,  
			xmlHttp.onreadystatechange=callback;
			//      Ajax  
			xmlHttp.send(null);
		}else {
			document.getElementById("userIdSpan").innerHTML = "";
		}
	}
	
	function callback() {
		if(xmlHttp.readyState == 4) { //Ajax       
			if (xmlHttp.status == 200) { //http    
				document.getElementById("userIdSpan").innerHTML = "<font color='red'>" + xmlHttp.responseText + "</font>";
			}else {
				alert("    ,   =" + xmlHttp.status);
			}
		}
	}

<input name="userId" type="text" class="text1" id="userId" size="10" maxlength="10" onkeypress="userIdOnKeyPress()" value="<%=userId %>" onblur="validate(this)"><span id="userIdSpan"></span>

user_validate.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@ page import="com.bjpowernode.drp.sysmgr.manager.*" %>    
<%	
	String userId = request.getParameter("userId");
	if (UserManager.getInstance().findUserById(userId) != null) {
		out.println("        ");
	}
%>