JSP页面
<td width="10%" class="main_matter_td">真实姓名</td>
<td width="40%" align="left" class="main_matter_td">
<input name="realnameLc" id="realnameLc" type="text" class="textBox" value="" size="24" onblur="isExist1(this.value)"/>
<div id="checkResult1">*</div>
JSP页面AJAX方法
<script type="text/javascript">
//验证用户名是否存在
var xmlHttp;
function isExist1()
{
var realname=document.getElementById("realnameLc");
if(window.ActiveXObject)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}else{
xmlHttp=new XMLHttpRequest();
}
var url="IsExsitDLUser1.action?realname="+document.getElementById("realnameLc").value.toString();
xmlHttp.onreadystatechange=handlStateChage;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
function handlStateChage()
{
if(xmlHttp.readyState==4) //代表请求完成,0=未初始化;1=正在加载;2=加载完成;3=交互中;4=完成
{
if(xmlHttp.status==200) //请求状态,200表示正常返回
{
document.getElementById("checkResult1").innerHTML =xmlHttp.responseText;
}
}
}
</script>
ACTION中方法:
/**
* 查看真实姓名是否重复
*
* @return
*/
public String IsExsitDLUser1() {
String str = get("realname").toString().trim();
str = str.replaceAll(" ", "");
System.out.println("IsExsitDLUser1str-->"+str);
if(str != "" || !("".equals(str))){
System.out.println("不为空");
if(this.ocs.isExsitUserName1(str)){
System.out.println("真实姓名已经存在,请重新注册");
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/xml;charset=UTF-8");
PrintWriter out= null;
try {
out = response.getWriter();
out.print("真实姓名已经存在,请重新注册");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
try {
System.out.println("该用户ming可以使用");
// this.getRequest().setAttribute("message", "该用户已经存在,请重新注册");
HttpServletResponse response = ServletActionContext
.getResponse();
response.setContentType("text/xml;charset=UTF-8");
PrintWriter out = null;
out = response.getWriter();
out.print("该用户ming可以使用");
} catch (Exception e) {
e.printStackTrace();
}
}
}else{
System.out.println("str-->"+str);
try {
System.out.println("真实姓名不能为空");
// this.getRequest().setAttribute("message", "该用户已经存在,请重新注册");
HttpServletResponse response = ServletActionContext
.getResponse();
response.setContentType("text/xml;charset=UTF-8");
PrintWriter out = null;
out = response.getWriter();
out.print("真实姓名不能为空");
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
这里是JAVASCRIPT和AJAX方法结合之后的异步实现用户名验证