微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ajax检测用户注册DEMO不使用框架

checkUser.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="GBK" %>
<%
	String[] userList={"明日科技","mr","wgh","mrsoft"};
	String user=new String(request.getParameter("user").getBytes("ISO-8859-1"),"GBK");			//获取用户名
	System.out.println("用户名:"+user);
	Arrays.sort(userList);								//对数组排序	
	int result=Arrays.binarySearch(userList,user);		//搜索数组
	if(result>-1){
		out.println("很抱歉,该用户名已经被注册!");		//输出检测结果
	}else{
		out.println("恭喜您,该用户名没有被注册!");		//输出检测结果
	}
%>

index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getcontextpath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>发送GET请求</title>
	<Meta http-equiv="pragma" content="no-cache">
	<Meta http-equiv="cache-control" content="no-cache">
	<Meta http-equiv="expires" content="0">    
	<link rel="stylesheet" type="text/css" href="CSS/style.css">
<script language="javascript">
	function checkUser(userName){
		if(userName.value==""){
			alert("请输入用户名!");userName.focus();return;
		}else{
			createRequest('checkUser.jsp?user='+userName.value);
		}
	}
    function createRequest(url) {
        HTTP_Request = false;
        if (window.XMLHttpRequest) {     // Mozilla……等非IE浏览器
            HTTP_Request = new XMLHttpRequest();	//创建XMLHttpRequest对象
        } else if (window.ActiveXObject) {     // IE浏览器
            try {
                HTTP_Request = new ActiveXObject("Msxml2.XMLHTTP");	//创建XMLHttpRequest对象
            } catch (e) {
                try {
                    HTTP_Request = new ActiveXObject("Microsoft.XMLHTTP");	//创建XMLHttpRequest对象
               } catch (e) {}
            }
        }
        if (!HTTP_Request) {
            alert("不能创建XMLHttpRequest对象实例!");
            return false;
        }
        HTTP_Request.onreadystatechange = getResult;     //调用返回结果处理函数
        HTTP_Request.open('POST',url,true);	//创建与服务器的连接
        HTTP_Request.send(null);		//向服务器发送请求
    }
	//返回结果处理函数
    function getResult() {
        if (HTTP_Request.readyState == 4) {     // 判断请求状态
            if (HTTP_Request.status == 200) {     // 请求成功,开始处理返回结果
                alert(HTTP_Request.responseText);	//显示判断结果
            } else {     //请求页面错误
                alert("您所请求的页面错误!");
            }
        }
    }
</script>
  </head>
  
  <body>
  <form name="form1" method="post" action="">
  <table width="402" border="0" align="center" cellpadding="0" cellspacing="1" bordercolor="#FFFFFF" bordercolorlight="#FFFFFF" bordercolordark="#777777" bgcolor="#999999">
    <tr>
      <td height="30" colspan="2" bgcolor="#EEEEEE">·用户注册</td>
    </tr>
    <tr>
      <td width="113" height="30" align="center" bgcolor="#FFFFFF">用 户 名:</td>
      <td width="387" bgcolor="#FFFFFF"><input name="username" type="text" id="username" size="30">
        <input name="b_checkUser" type="button" class="btn_grey" id="b_checkUser" value="检测用户名" onClick="checkUser(this.form.username);"></td>
    </tr>
    <tr>
      <td height="30" align="center" bgcolor="#FFFFFF">密码:</td>
      <td bgcolor="#FFFFFF"><input name="pwd" type="password" id="pwd" size="30"></td>
    </tr>
    <tr>
      <td height="30" align="center" bgcolor="#FFFFFF">确认密码:</td>
      <td bgcolor="#FFFFFF"><input name="pwd1" type="password" id="pwd1" size="30"></td>
    </tr>
    <tr>
      <td height="40" colspan="2" align="center" bgcolor="#FFFFFF"><input name="b_submit" type="submit" class="btn_grey" id="b_submit" value="提交">
        
      <input name="b_reset" type="reset" class="btn_grey" id="b_reset" value="重置"></td>
    </tr>
  </table>
  </form>  
  </body>
  
</html>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐