日期:2014-05-16 浏览次数:20698 次
<script type = "text/javascript" charset="utf-8"> var XMLHttp ; function createXMLHttpRequest(){ if(window.ActiveObject){ XMLHttp= new ActiveObject("Microsoft.XMLHTTP"); }else if(window.XMLHttpRequest) { XMLHttp = new XMLHttpRequest(); } } function checkUserName(){ createXMLHttpRequest(); var name = document.getElementById("uname").value; if(name==""){ alert("用户名不能为空"); return; } XMLHttp.open("get","findName?uname="+name,true); XMLHttp.onreadystatechange=processRequest; XMLHttp.send(null); } function processRequest(){ if(XMLHttp.readyState==4){ if(XMLHttp.status==200){ if(XMLHttp.responseText.trim()=="false"){ alert("存在"); } else if(XMLHttp.responseText.trim()=="true"){ alert("可用"); } } } } </script>
public class findName extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset = gbk"); request.setCharacterEncoding("GBK"); PrintWriter out = response.getWriter(); String name = request.getParameter("uname"); UserInfoBiz ubiz = new UserInfoBizImpl(); int number = ubiz.getUsersByName(name); if(number>0){ out.print(false); } else{ out.print(true); } out.flush(); out.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
String.prototype.trim = function () { return this.replace(/^\s*|\s*$/g, ''); }