<script type="text/javascript">
function checkBrowser() {
try {
return new XMLHttpRequest();
} catch (e) {
try {
return ActiveXObject("Msxm12.XMLHTTP");
} catch (e) {
try {
return ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
alert("Your browser is not supported!");
throw e;
}
}
}
}
window.onload = function () {
var username = document.getElementById("loginname");
username.onblur = function () {
var xmlHttp = checkBrowser();
xmlHttp.open("POST", "<c:url value='/ajaxValidateLoginname'/>", true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send("loginname=" + username.value);
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
var text = xmlhttp.responseText;
var label = document.getElementById("loginnameError");
if (text === false) {
label.innerHTML = "The user name has been registered!";
}else{
label.innerHTML = "";
}
}
};
};
};
This is a servlet used to check if the login name already exists.
public void doPost ajaxValidateLoginname(HttpServletRequest request, HttpServletResponse response) throws IOException {
String loginname = request.getParameter("loginname");
boolean flag = us.ajaxValidateLoginname(loginname);
response.getWriter().print(flag);
return null;
}