自动检索用户名重复性

来源:互联网 发布:java环境变量配置win7 编辑:程序博客网 时间:2024/05/22 08:29

1.checkUsername.jsp

<%@page language="java" pageEncoding="UTF-8"%>
<%
   response.setContentType("text/xml");
   response.setHeader("Cache-Control", "no-cache");
   String username = request.getParameter("username");
   boolean isValid = false;
   if(username.equals("admin")){
       isValid = true;
   }
   if(isValid){
       out.println("<content>该用户已经存在!</content>");
   }else{
       out.println("<content>ok</content>");
   }
%>


2.register.html

<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script>
        var xmlHttpReq = false;
        function createXMLHttpRequest(){
            try{
                  //Firefox,Opera8.0+,Safari
                  xmlHttpReq = new XMLHttpRequest();
              }catch(e){
                  //Internet Explorer
                  try{
                      //Internet Explorer 6.0
                      xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
                  }catch(e){
                      try{
                           //Internet Explorer 5.5
                          xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
                      }catch(e){
                          alert("/u4f60的浏览器不支持AJAX!");
                          return false;
                      }
                  }
              }
        }
        function send(url,responseMethod){
            createXMLHttpRequest();
            xmlHttpReq.open("post",url,true);
            xmlHttpReq.onreadystatechange=responseMethod;//指定响应的函数
            xmlHttpReq.send(null);//发送请求
        }
        //------------------------
        function checkUsername(){
            var username = document.getElementById("username").value;
            if(username==""){
                alert("/u8bf7输入用户名");
                return false;
            }else{
                send('/AjaxApplication/checkUsername.jsp?username='+username,parse);
            }
        }
        function parse(){
            if(xmlHttpReq.readyState==4){//对象状态
                if(xmlHttpReq.status==200){//信息已经成功返回,开始处理信息
                    var res = xmlHttpReq.responseXML.getElementsByTagName("content")[0].firstChild.data;
                    if(res=="ok"){
                        document.getElementById("status").innerHTML="该用户名可以使用!";
                    }else{
                        document.getElementById("status").innerHTML=res;
                        alert(res);
                    }
                }else{
                    alert("/u6240请求的页面有异常");
                }
            }
        }
    </script>
  </head>
  <body>
      用户名:<input type="text" name="name" id="username" size="10" onblur="checkUsername();">
      <span id="status">
      </span>
  </body>
</html>


原创粉丝点击