Ajax+Jsp+Access实现的唯一性校验例子

如标题,Ajax+ Jsp+Access实现的唯一性校验例子,这里主要说明的是Ajax的原理,为了使得大家把例子下载下来就能运行,所以使用Access数据库,而且不需要配置数据源头,使用Tomcat发布就可以啦!
主要代码如下:
check.js:

Java代码 复制代码
  1. /**
  2. *@authorfuhao
  3. */
  4. varhttp_request=false;
  5. //向服务器发起XMLHTTP请求
  6. functionsend_request(){
  7. //获得文本框里面输入的用户名
  8. varloginname=document.getElementById("userName").value;
  9. //要请求的服务器地址
  10. url="check.jsp?userName="+loginname;
  11. http_request=false;
  12. //开始初始化XMLHttpRequest对象
  13. if(window.XMLHttpRequest){//说明是Mozila浏览器
  14. http_request=newXMLHttpRequest();
  15. if(http_request.ovverideMimeType){//设置MiME类别
  16. http_request.ovverideMimeType('text/xml');
  17. }
  18. }
  19. elseif(window.ActiveXObject){//说明是IE浏览器
  20. try{
  21. http_request=newActiveXObject("Msxml2.XMLHTTP");
  22. }catch(e){
  23. try{
  24. http_request=newActiveXObject("Microsoft.XMLHTTP");
  25. }catch(e){}
  26. }
  27. }
  28. if(!http_request){//异常,创建对象实例失败
  29. alert("创建XMLHttpRequest对象失败");
  30. returnfalse;
  31. }
  32. http_request.onreadystatechange=callback;
  33. //确定发送请求的方式和URL
  34. http_request.open("GET",url,true);
  35. http_request.send(null);
  36. }
  37. //处理返回信息的函数
  38. functioncallback(){
  39. if(http_request.readystate==4){//判断对象状态
  40. if(http_request.status==200){//说明信息已经成功的返回
  41. displays();
  42. }else{
  43. alert("从服务器返回的状态是:"+http_request.statusText);
  44. }
  45. }else{
  46. document.getElementById("div").style.display="";
  47. }
  48. }
  49. functiondisplays(){
  50. vardiv=document.getElementById("div");
  51. div.innerHTML=http_request.responseText;
  52. }
  53. functiondocheck(){
  54. varloginname=document.getElementById("userName").value;
  55. document.getElementById("div").style.display="none";
  56. if(loginname==""){//判断文本框是否为空
  57. document.getElementById("div").style.display="none";
  58. returnfalse;
  59. }else{
  60. document.getElementById("div").style.display="";
  61. //为了观察效果,设置延迟
  62. setTimeout(send_request,3000);
  63. }
  64. }
服务器端代码,check.jsp:
Java代码 复制代码
  1. <%@pagelanguage="java"contentType="text/html;charset=GBK"
  2. pageEncoding="GBK"import="java.sql.*"%>
  3. <!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <metahttp-equiv="Content-Type"content="text/html;charset=GBK">
  7. <title>Inserttitlehere</title>
  8. </head>
  9. <body>
  10. <%
  11. //获得客户端传来的用户名
  12. StringuserName=request.getParameter("userName");
  13. System.out.println("useName:"+userName);
  14. //获得Access数据库的绝对路径
  15. Stringrealpath="data/database.mdb";
  16. //获得Access数据库的相对路径
  17. Stringdbpath=application.getRealPath(realpath);
  18. //设置数据库连接的字符串
  19. Stringurl="jdbc:odbc:driver={MicrosoftAccessDriver(*.mdb)};DBQ="+dbpath;
  20. //加载驱动程序
  21. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  22. //建立数据库连接
  23. Connectionconn=DriverManager.getConnection(url);
  24. //创建语句对象
  25. Statementstmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
  26. Stringsql="";
  27. //创建查询数据库的SQL语句
  28. sql="select*fromuserwhereuser_Name='"+userName+"'";
  29. System.out.println("sql:"+sql);
  30. //得到数据集
  31. ResultSetrs=stmt.executeQuery(sql);
  32. if(rs.next()){
  33. out.println("对不起,该用户名已经被注册了");
  34. }else{
  35. out.println("恭喜你,该用户名可以注册");
  36. }
  37. //关闭数据库连接
  38. rs.close();
  39. stmt.close();
  40. conn.close();
  41. %>
  42. </body>
  43. </html>

客户端代码,index.html:
Java代码 复制代码
  1. <!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <metahttp-equiv="Content-Type"content="text/html;charset=GBK">
  5. <title>测试校验</title>
  6. <LINKmedia=allhref="css/style.css"type=text/cssrel=stylesheet>
  7. <scripttype="text/javascript"src="js/check.js"></script>
  8. </head>
  9. <body>
  10. <formaction=""method="">
  11. <br/><br/><br/><palign="center"><fontcolor="green"size="4">Ajax+Jsp+Access唯一性校验例子</font></p>
  12. <tablealign='center'border='1'bordercolor='#8CB3E3'width="55%"cellpadding='0'cellspacing='0'>
  13. <tbodyid="tbodyid">
  14. <tr>
  15. <tdnowrapclass="data_tab_tdr"width="10%">用户名:</td>
  16. <tdclass="data_tab_tdl"width="15%">
  17. <inputtype="text"name="userName"size="25"id="userName"οnchange="docheck()"/>
  18. </td>
  19. <tdclass="data_tab_tdl"width="15%"><divid="div"style="display:none"><imgsrc="images/ajax-loader.gif"></div></td>
  20. </tr>
  21. </tbody>
  22. </table>
  23. </form>
  24. </body>
  25. </html>
 ajaxCheck.zip (26.3 KB) 
  • 描述: 全部代码,使用Tomcat5.0+IE浏览器发布即可
  • 下载次数: 215
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值