ajax实现注册页面动态验证用户名是否已注册,不必提交即可验证

来源:互联网 发布:阿里云 centos 中文 编辑:程序博客网 时间:2024/06/05 01:01

今天学了一下ajax,感觉很爽啊。ajax真是很强大、

我首先就把我之前一直没解决的问题:如何在前台动态验证用户名是否已注册,而不必提交刷新之后再验证,上代码:

首先,jsp页面:

[java] view plaincopyprint?
  1. <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>  
  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>My JSP 'index.jsp' starting page</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.   </head>  
  22.     
  23.   <body>  
  24.    <input type="text" name="text1" id="text1id"  onblur="Ajaxtest1();"><br>  
  25.    <input type="text" name="text2" id="text2id" /><br>  
  26. <%--      <input type="button" value="button" onclick="Ajaxtest1();"><br>--%>  
  27.    <div id="div1"></div>  
  28.   </body>  
  29. </html>  

这个其实只是两个文本框,输入用、

下面是js代码。验证用的、

[html] view plaincopyprint?
  1. <script type="text/javascript">  
  2.     var xmlhttprequest=null;  
  3.         function Ajaxtest1(){  
  4.         if(window.ActiveXObject){//IE浏览器  
  5.                 xmlhttprequest=new ActiveXObject("Microsoft.XMLHTTP");  
  6.             }  
  7.         else if(window.XMLHttpRequest){  
  8.                 xmlhttprequest=new XMLHttpRequest();  
  9.             }  
  10.         if(null!=xmlhttprequest){  
  11.             var text1=document.getElementById("text1id").value;  
  12.             var text2=document.getElementById("text2id").value;  
  13.                 xmlhttprequest.open("GET", "testajax1?t1="+text1+"&t2="+text2, true);  
  14.                 xmlhttprequest.onreadystatechange=ajaxcallback;  
  15.                 //var tt=document.getElementById("text1").innerHTML;  
  16.                 xmlhttprequest.send(null);  
  17.             }  
  18.     }  
  19.         function ajaxcallback(){  
  20.                 if(xmlhttprequest.readyState==4){  
  21.             if(xmlhttprequest.status==200){  
  22.                     var text=xmlhttprequest.responseText;  
  23.                     document.getElementById("div1").innerHTML=text;  
  24.                 }  
  25.                     }  
  26.             }  
  27.     </script>  
这个没什么可说的,唯一注意的一点就是xmlhttprequest.open("GET", "testajax1?t1="+text1+"&t2="+text2, true);

这里面的testajax1是servlet,注意这个地址需和web.xml保持一致。

接下来是testajax1.java

[java] view plaincopyprint?
  1. package com.guang.ajax;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.PrintWriter;  
  5.   
  6. import javax.servlet.ServletException;  
  7. import javax.servlet.http.HttpServlet;  
  8. import javax.servlet.http.HttpServletRequest;  
  9. import javax.servlet.http.HttpServletResponse;  
  10.   
  11. import com.guang.sqlhelp.sqlhelp;  
  12. import java.sql.*;  
  13. public class testajax1 extends HttpServlet {  
  14.     public testajax1() {  
  15.         super();  
  16.     }  
  17.     int i=0;  
  18.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  19.             throws ServletException, IOException {  
  20.         request.setCharacterEncoding("gbk");  
  21.         response.setContentType("text/html;charset=gbk");  
  22.         PrintWriter out = response.getWriter();  
  23.         //System.out.println("doGet"+i);  
  24.         i++;  
  25.         String t1=request.getParameter("t1");  
  26.         //System.out.println(t1);  
  27.         //out.println("Hello World");  
  28.         sqlhelp sp=new sqlhelp();  
  29.         Connection conn=sp.getconn();  
  30.         Statement sta=sp.getsta2(conn);  
  31.         String sql="select * from baseinfo where usernum='"+t1+"'";  
  32.         ResultSet set=sp.getset(sta, sql);  
  33.         int j=0;  
  34.         if(set!=null){  
  35.         try {  
  36.             set.last();  
  37.         } catch (SQLException e) {  
  38.             e.printStackTrace();  
  39.         }  
  40.         try {  
  41.             j=set.getRow();  
  42.         } catch (SQLException e) {  
  43.             e.printStackTrace();  
  44.         }  
  45.         if(j==1){  
  46.             out.println("此学号已被注册,请换一个!!");  
  47.         }  
  48.         else{  
  49.             out.println("恭喜您,这个可以注册!");  
  50.         }  
  51.         out.close();  
  52.     }  
  53.         }  
  54.   
  55. }  

0 0
原创粉丝点击