加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Ajax实现异步验证用户是否存在

发布时间:2020-12-16 00:55:49 所属栏目:百科 来源:网络整理
导读:在Index.jsp页面中 //使用ajax进行异步校验,校验登录名在数据库是否存在 //创建ajax引擎 function createXmlHttpRequest(){ var xmlHttp; try{ //Firefox,Opera 8.0+,Safari xmlHttp=new XMLHttpRequest(); }catch (e){ try{ //Internet Explorer xmlHttp=ne
在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(); }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读