通用的Ajax模板 校验用户名

Ajax.js

Javascript代码 复制代码
  1. var _xmlHttpRequestObj;   
  2. var _loadingFunction;   
  3. var _postHttpProcessChangeCallback;   
  4. var resultValue;   
  5.   
  6.   
  7. function postXmlHttp(submiturl,callbackfunc,loadfunc)   
  8. {   
  9.     _postHttpProcessChangeCallback = callbackfunc;   
  10.     _loadingFunction=loadfunc;   
  11.     if(window.createRequest)   
  12.     {   
  13.         _xmlHttpRequestObj = window.createRequest();   
  14.         _xmlHttpRequestObj.open('POST',submiturl,true);   
  15.         _xmlHttpRequestObj.onreadystatechange=postXmlHttpProcessPostChange;   
  16.         _xmlHttpRequestObj.send();   
  17.     }else if(window.XMLHttpRequest)   
  18.     {   
  19.         _xmlHttpRequestObj = new XMLHttpRequest();   
  20.         _xmlHttpRequestObj.overrideMimeType('text/xml');   
  21.         _xmlHttpRequestObj.open('POST',submiturl,true);   
  22.         _xmlHttpRequestObj.onreadystatechange=postXmlHttpProcessPostChange;   
  23.         _xmlHttpRequestObj.send("");   
  24.     }else if(window.ActiveXObject)   
  25.     {   
  26.         _xmlHttpRequestObj = new ActiveXObject("Microsoft.XMLHTTP");   
  27.         _xmlHttpRequestObj.open('POST',submiturl,true);   
  28.         _xmlHttpRequestObj.onreadystatechange=postXmlHttpProcessPostChange;   
  29.         _xmlHttpRequestObj.send();   
  30.     }   
  31.        
  32.     
  33. }   
  34.   
  35. function postXmlHttpProcessPostChange()   
  36. {   
  37.     if(_xmlHttpRequestObj.readyState==4&&_xmlHttpRequestObj.status==200)   
  38.     {   
  39.         resultValue = _xmlHttpRequestObj.responseText;   
  40.         setTimeout(_postHttpProcessChangeCallback,2);   
  41.     }   
  42.     if(_xmlHttpRequestObj.readyState==1)   
  43.     {   
  44.         setTimeout(_loadingFunction,2);   
  45.     }   
  46. }  
var _xmlHttpRequestObj;
var _loadingFunction;
var _postHttpProcessChangeCallback;
var resultValue;


function postXmlHttp(submiturl,callbackfunc,loadfunc)
{
    _postHttpProcessChangeCallback = callbackfunc;
    _loadingFunction=loadfunc;
    if(window.createRequest)
    {
    	_xmlHttpRequestObj = window.createRequest();
    	_xmlHttpRequestObj.open('POST',submiturl,true);
    	_xmlHttpRequestObj.onreadystatechange=postXmlHttpProcessPostChange;
    	_xmlHttpRequestObj.send();
    }else if(window.XMLHttpRequest)
    {
    	_xmlHttpRequestObj = new XMLHttpRequest();
    	_xmlHttpRequestObj.overrideMimeType('text/xml');
    	_xmlHttpRequestObj.open('POST',submiturl,true);
    	_xmlHttpRequestObj.onreadystatechange=postXmlHttpProcessPostChange;
    	_xmlHttpRequestObj.send("");
    }else if(window.ActiveXObject)
    {
        _xmlHttpRequestObj = new ActiveXObject("Microsoft.XMLHTTP");
        _xmlHttpRequestObj.open('POST',submiturl,true);
        _xmlHttpRequestObj.onreadystatechange=postXmlHttpProcessPostChange;
        _xmlHttpRequestObj.send();
    }
    
 
}

function postXmlHttpProcessPostChange()
{
   	if(_xmlHttpRequestObj.readyState==4&&_xmlHttpRequestObj.status==200)
   	{
   	    resultValue = _xmlHttpRequestObj.responseText;
   	    setTimeout(_postHttpProcessChangeCallback,2);
   	}
   	if(_xmlHttpRequestObj.readyState==1)
   	{
   		setTimeout(_loadingFunction,2);
   	}
}



JSP页面

Html代码 复制代码
  1. <%@ page language="java" pageEncoding="utf-8"%>  
  2. <%   
  3. String path = request.getContextPath();   
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";   
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.        
  12.     <title>AjaxApp</title>  
  13.     <meta http-equiv="pragma" content="no-cache">  
  14.     <meta http-equiv="cache-control" content="no-cache">  
  15.     <meta http-equiv="expires" content="0">       
  16.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  17.     <meta http-equiv="description" content="This is my page">  
  18.     <!--  
  19.     <link rel="stylesheet" type="text/css" href="styles.css">  
  20.     -->  
  21.        
  22.     <script type="text/javascript" src="./ajax.js"></script>  
  23.   
  24.        
  25.     <script type="text/javascript">  
  26.             
  27.          function checkUserName()   
  28.          {   
  29.             var userName = document.getElementsByName("userName")[0].value;   
  30.             postXmlHttp("./UserServlet.do?userName="+userName,"callback(resultValue)","loadingFunc()");   
  31.          }   
  32.             
  33.          function callback(resultValue1)   
  34.          {   
  35.             var td = document.getElementById("td");   
  36.             td.innerHTML=resultValue1;   
  37.          }   
  38.             
  39.          function loadingFunc()   
  40.          {   
  41.             var td = document.getElementById("td");   
  42.             td.innerHTML="loading..."  
  43.          }   
  44.     </script>  
  45.   </head>  
  46.      
  47.   <body>  
  48.     <form action="" method="post">  
  49.        <table align="center" border="0">  
  50.           <tr>  
  51.             <td>用户名</td><td><input name="userName"></td><td id="td"></td>  
  52.           </tr>  
  53.           <tr>  
  54.             <td>密  码</td><td><input name="password" onclick="checkUserName();"></td>  
  55.           </tr>  
  56.           <tr>  
  57.             <td colspan="2"><input type="submit" value="提交"></td>  
  58.           </tr>  
  59.        </table>  
  60.     </form>  
  61.   </body>  
  62. </html>  
<%@ page language="java" pageEncoding="utf-8"%>
<%
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>AjaxApp</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	
	<script type="text/javascript" src="./ajax.js"></script>

	
	<script type="text/javascript">
	     
	     function checkUserName()
	     {
	        var userName = document.getElementsByName("userName")[0].value;
	        postXmlHttp("./UserServlet.do?userName="+userName,"callback(resultValue)","loadingFunc()");
	     }
	     
	     function callback(resultValue1)
	     {
	        var td = document.getElementById("td");
	        td.innerHTML=resultValue1;
	     }
	     
	     function loadingFunc()
	     {
	        var td = document.getElementById("td");
	        td.innerHTML="loading..."
	     }
	</script>
  </head>
  
  <body>
    <form action="" method="post">
       <table align="center" border="0">
          <tr>
            <td>用户名</td><td><input name="userName"></td><td id="td"></td>
          </tr>
          <tr>
            <td>密  码</td><td><input name="password" οnclick="checkUserName();"></td>
          </tr>
          <tr>
            <td colspan="2"><input type="submit" value="提交"></td>
          </tr>
       </table>
    </form>
  </body>
</html>



Serlet类

Java代码 复制代码
  1. public class UserServlet extends HttpServlet   
  2. {   
  3.   
  4.     /**  
  5.      * Destruction of the servlet. <br>  
  6.      */  
  7.     public void destroy()   
  8.     {   
  9.         super.destroy(); // Just puts "destroy" string in log   
  10.         // Put your code here   
  11.     }   
  12.   
  13.   
  14.     public void doPost(HttpServletRequest request, HttpServletResponse response)   
  15.             throws ServletException, IOException   
  16.     {          
  17.         String userName = (String)request.getParameter("userName");   
  18.         UserService userService = new UserService();   
  19.         boolean b = userService.checkUserName1(userName);   
  20.         response.setCharacterEncoding("utf-8");   
  21.                   //jsp页面编码最好也用utf-8 防止乱码   
  22.         PrintWriter pw= response.getWriter();   
  23.         if(b)   
  24.         {   
  25.             pw.write("<font color=red>该用户名已经被使用</font>");   
  26.         }else  
  27.         {   
  28.             pw.write("<font color=red>该用户名可用</font>");   
  29.         }   
  30.            
  31.     }   
  32. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值