在Index.jsp页面中
//使用ajax进行异步校验,校验登录名在数据库是否存在
//创建ajax引擎
function createXmlHttpRequest(){
var xmlHttp;
try{ //Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}catch (e){
try{ //Internet Explorer
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){}
}
}
return xmlHttp;
}
function checkLogonName(){
var logonName = document.getElementById("logonName").value;
//第一步:创建ajax引擎
var xmlHttp = createXmlHttpRequest();
//第二步:事件处理函数,实质上相当一个监听,监听服务器与客户端的连接状态
xmlHttp.onreadystatechange = function(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
var data = xmlHttp.responseText;
//alert(data);
if(data==1){
alert("当前输入的登录名["+logonName+"]已被其他人占用,请重重新输入!");
document.getElementById("logonName").value = "";
document.getElementById("logonName").focus();
}
}
}
}
//第三步:与后台服务器建立一个连接
xmlHttp.open("post","../../checkLogonName",true);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
//第四步:发送请求的参数
xmlHttp.send("logonName="+logonName);
}
在servlet页面中:
IElecUserService elecUserService = (IElecUserService) ServiceProvider.getService(IElecUserService.SERVICE_NAME);
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String logonName = request.getParameter("logonName");
/**
* checkflag:判断当前登录名在数据库中是否存在的标识
* checkflag=1:如果值为1,说明当前登录名在数据库中有重复记录,则不能进行保存
* checkflag=2:如果值为2,说明当前登录名在数据库中没有重复值,可以进行保存
*/
String checkflag = elecUserService.checkLogonName(logonName);
out.print(checkflag);
out.flush();
out.close();
}
//使用ajax进行异步校验,校验登录名在数据库是否存在
//创建ajax引擎
function createXmlHttpRequest(){
var xmlHttp;
try{ //Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}catch (e){
try{ //Internet Explorer
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){}
}
}
return xmlHttp;
}
function checkLogonName(){
var logonName = document.getElementById("logonName").value;
//第一步:创建ajax引擎
var xmlHttp = createXmlHttpRequest();
//第二步:事件处理函数,实质上相当一个监听,监听服务器与客户端的连接状态
xmlHttp.onreadystatechange = function(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
var data = xmlHttp.responseText;
//alert(data);
if(data==1){
alert("当前输入的登录名["+logonName+"]已被其他人占用,请重重新输入!");
document.getElementById("logonName").value = "";
document.getElementById("logonName").focus();
}
}
}
}
//第三步:与后台服务器建立一个连接
xmlHttp.open("post","../../checkLogonName",true);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
//第四步:发送请求的参数
xmlHttp.send("logonName="+logonName);
}
在servlet页面中:
IElecUserService elecUserService = (IElecUserService) ServiceProvider.getService(IElecUserService.SERVICE_NAME);
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String logonName = request.getParameter("logonName");
/**
* checkflag:判断当前登录名在数据库中是否存在的标识
* checkflag=1:如果值为1,说明当前登录名在数据库中有重复记录,则不能进行保存
* checkflag=2:如果值为2,说明当前登录名在数据库中没有重复值,可以进行保存
*/
String checkflag = elecUserService.checkLogonName(logonName);
out.print(checkflag);
out.flush();
out.close();
}