日期:2014-05-16 浏览次数:20568 次
<script language="javascript"> //发送请求函数 function send(url){ createXMLHttpRequest(); XMLHttpReq.open("get",url,true); XMLHttpReq.onreadystatechange=proce; //指定响应的函数 XMLHttpReq.send(null); //发送请求 } function proce(){ if(XMLHttpReq.readyState==4){ //对象状态 if(XMLHttpReq.status==200){//信息已成功返回,开始处理信息 <!--测试读取xml开始--> var root=XMLHttpReq.responseXML; var res=root.getElementsByTagName("content")[0].firstChild.data; window.alert(res); <!--测试读取xml结束--> //var xmlReturn = XMLHttpReq.responseText; //window.alert(xmlReturn); }else{ window.alert("所请求的页面有异常"); } } } //身份验证 function check(){ var name=document.getElementById("name").value; if(name==""){ alert("请输入姓名!"); return false; } else{ document.getElementById("load").style.display=''; send('user.do?method=checkUser&name='+name); document.getElementById("load").style.display='none'; } } </script>
<input type="text" name="name" id="name" onblur="check()"> <div id=load style="display:none; position:absolute;right:0px;top:0px;background:#FF5B5B;border:1px solid">正在验证用户名,请稍后^_^</div>
public ActionForward checkUser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { try { request.setCharacterEncoding("gb2312"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } try{ String msgStr=""; response.setContentType("text/xml;charset=GB2312"); response.setHeader("Cache-Control","no-cache"); String name=(String)request.getParameter("name"); System.out.println("name = "+name); int flag = userManager.add_CheckUser(name); if(flag == 1){ //找查数据库中有无该用户名 msgStr ="对不起,此用户名已经存在,请更换用户名注册!"; }else{ msgStr ="用户未被注册,可以使用!"; } response.getWriter().println("<?xml version='1.0' encoding='GB2312' ?>"); response.getWriter().println("<root>"); response.getWriter().println("<content>"); response.getWriter().print(msgStr); response.getWriter().println("</content>"); response.getWriter().println("</root>"); response.getWriter().close(); }catch(Exception ex){ } return null; }