随笔-75  评论-74  文章-35  trackbacks-1
  1. <html>      
  2.     <head>      
  3.         <title>这是一个简单登陆验证也可以改为检查用户名是否存在的ajax例子(可以自由扩张)</title>      
  4.         <script type= "text/javascript" >      
  5.             var xmlHttpReq;      
  6.              //创建XMLHTTP对象      
  7.             function createXMLHttpRequest(){      
  8.                  if (window.ActiveXObject){      
  9.                     xmlHttpReq =  new  ActiveXObject( "MSXML2.XMLHTTP.3.0" );      
  10.                 } else {      
  11.                     xmlHttpReq =  new  XMLHttpRequest();      
  12.                 }      
  13.             }      
  14.              //检查用户      
  15.             function checkUser(){      
  16.                 createXMLHttpRequest();      
  17.                 var userName =document.getElementById( "userName" ).value;      
  18.                 var password =document.getElementById( "password" ).value;      
  19.                  //处理检查的地址      
  20.                 var url= "checkUser.jsp?userName=" +userName+ "&password=" +password;      
  21.                 xmlHttpReq.open( "GET" ,url, true );      
  22.                 xmlHttpReq.onreadystatechange = showResult; //showResult是当onreadystatechange的值改变时触发的javascript函数      
  23.                 xmlHttpReq.send();      
  24.             }      
  25.                   
  26.             function showResult(){      
  27.                  if (xmlHttpReq.readyState ==  4 ){      
  28.                      if (xmlHttpReq.status ==  200 ){      
  29.                         var result = xmlHttpReq.responseText; //将响应信息作为字符串返回     
  30.                          //alert( result);   
  31.                         var checkResult = document.getElementById( "checkResult" ).innerHTML= "<b>" +result+ "</b>" ;   
  32.                          //var b=document.createElement("b");     
  33.                          //var resultContent = document.createTextNode(result);   
  34.                         // b.appendChild(resultContent);   
  35.                         // checkResult.appendChild(b);   
  36.                     }      
  37.                 }      
  38.             }      
  39.         </script>      
  40.     </head>      
  41.     <body>      
  42.         用户名:<input type=text " id=" userName"><p>      
  43.         密  码:<input type=text " id=" password"><p>      
  44.         <input type= "button"  value= "提交"  onclick= "checkUser()" ><p>      
  45.         <div id= "checkResult" ></div>      
  46.     </body>      
  47. </html> 

 

  1. <%@ page contentType= "text/html;charset=gb2312"  %>   
  2. <%   
  3. String userName=request.getParameter( "userName" );   
  • String password=request.getParameter( "password" );   
  • System.out.println( "userName=" +userName);   
  • if (userName!=  null  && password!= null ){   
  •      if (userName.equals( "huanglq" ) && password.equals( "password" )){   
  •         out.println( "<font color='green' >验证成功</font>" );   
  •     } else {   
  •         out.println( "<font color='red' >用户名或密码错误</font>" );   
  •     }   
  • }   
  • %>  
  •  

    posted on 2008-04-02 22:55 影子 阅读(188) 评论(0)  编辑  收藏 所属分类: 学习笔记