Chinaunix首页 | 论坛 | 博客
  • 博客访问: 96048
  • 博文数量: 34
  • 博客积分: 925
  • 博客等级: 准尉
  • 技术积分: 350
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-15 11:52
文章分类

全部博文(34)

文章存档

2011年(34)

我的朋友

分类: WINDOWS

2011-08-29 17:05:25

1. checkUser.jsp
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

  2.     <%
  3.         String name = request.getParameter("name");
  4.         String msg = "恭喜你,该用户可以注册!";
  5.         if("admin".equals(name)){
  6.             msg = "Sorry, The user name is already exist.";
  7.         }
  8.         out.print(msg);
  9.     %>
test_ajax1.jsp
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>

  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8.   <head>
  9.     <base href="<%=basePath%>">
  10.     
  11.     <title>My JSP 'test_ajax1.jsp' starting page</title>
  12.     
  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.     
  20.     <script type="text/javascript">
  21.     function createXmlHttpRequest(){
  22.         var xmlhttp = null;
  23.         try{
  24.                 //Firefox, Opera 8.0+, Safari
  25.          xmlhttp=new XMLHttpRequest();
  26.                 }catch(e){//IEIE7.0以下的浏览器以ActiveX组件的方式来创建XMLHttpRequest对象
  27.         var MSXML =
  28.         ['MSXML2.XMLHTTP.6.0','MSXML2.XMLHTTP.5.0',
  29.         'MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0',
  30.         'MSXML2.XMLHTTP','Microsoft.XMLHTTP'];
  31.          for(var n = 0; n < MSXML.length; n ++){
  32.          try{
  33.          xmlhttp = new ActiveXObject(MSXML[n]);
  34.          break;
  35.          }catch(e){}}
  36.          }
  37.         return xmlhttp;
  38.     }
  39.     
  40.         function checkUserByAjax(){
  41.             var name = document.getElementById("name").value;
  42.             // 第一步:得到一个XMLHttpRequest对象
  43.             //var xhr = new new XMLHttpRequest();
  44.             // 如果ie6需要使用到ActiveX组建
  45.             var xhr = createXmlHttpRequest();
  46.             // 第二步,准备一个链接请求
  47.             
  48.             xhr.open("get","checkUser.jsp?name="+ name, true);
  49.             //第三步,发起请求
  50.             xhr.send(null);
  51.             xhr.onreadystatechange = function(){
  52.                 //var smsg = document.getElementById("statusMsg");
  53.                 //smsg.innerHTML += "readyState:" + xhr.readyState + ",status: " + xhr.status+ "
    "
    ;
  54.                 //4表示加载完成
  55.                 if(xhr.readyState == 4){
  56.                     if(xhr.status == 200 || xhr.status == 304){
  57.                         //获取结果
  58.                         document.getElementById("msg").innerHTML = xhr.responseText;
  59.                     }
  60.                 }
  61.             }
  62.         }
  63.     </script>
  64.   </head>
  65.   
  66.   <body>
  67.     
  68.     
  69.     <form action="" method="post">
  70.         <p>用户名:
  71.             <input type="text" name="name" id="name"> <input type="button" value="Check User Name" onclick="checkUserByAjax();">
  72.             
  73.         </p>
  74.         <div id="msg"></div>
  75.         <div id="statusMsg"></div>
  76.     </form>
  77.   </body>
  78. </html>

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>

  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8.   <head>
  9.     <base href="<%=basePath%>">
  10.     
  11.     <title>My JSP 'test_ajax1.jsp' starting page</title>
  12.     
  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.     
  20.     <script type="text/javascript">
  21.     function createXmlHttpRequest(){
  22.         var xmlhttp = null;
  23.         try{
  24.                 //Firefox, Opera 8.0+, Safari
  25.          xmlhttp=new XMLHttpRequest();
  26.                 }catch(e){//IEIE7.0以下的浏览器以ActiveX组件的方式来创建XMLHttpRequest对象
  27.         var MSXML =
  28.         ['MSXML2.XMLHTTP.6.0','MSXML2.XMLHTTP.5.0',
  29.         'MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0',
  30.         'MSXML2.XMLHTTP','Microsoft.XMLHTTP'];
  31.          for(var n = 0; n < MSXML.length; n ++){
  32.          try{
  33.          xmlhttp = new ActiveXObject(MSXML[n]);
  34.          break;
  35.          }catch(e){}}
  36.          }
  37.         return xmlhttp;
  38.     }
  39.     
  40.         function checkUserByAjax(){
  41.             var name = document.getElementById("name").value;
  42.             // 第一步:得到一个XMLHttpRequest对象
  43.             //var xhr = new new XMLHttpRequest();
  44.             // 如果ie6需要使用到ActiveX组建
  45.             var xhr = createXmlHttpRequest();
  46.             // 第二步,设置一个针对readystatechange的事件的监听函数
  47.             xhr.onreadystatechange = function(){
  48.                 if(xhr.readyState == 4){
  49.                     if(xhr.status == 200 || xhr.status == 304){
  50.                         // 监听函数处理返回的结果
  51.                         document.getElementById("msg").innerHTML = xhr.responseText;
  52.                         // 如果用户已经存在需要把光标重新设置到name,填写
  53.                         var ret = xhr.responseText;
  54.                         document.getElementById("msg").innerHTML = ret;
  55.                         if(ret == "Name can be used."){
  56.                             document.getElementById("name").focus();
  57.                         }
  58.                     }
  59.                 }
  60.             }
  61.             // 第三步,准备一个post请求
  62.             xhr.open("post","checkUser.jsp?name="+ name, true);
  63.             //第四步,发起请求
  64.             // 设置请求头
  65.             xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  66.             xhr.send("name="+name+"&password=pp");
  67.         }
  68.     </script>
  69.   </head>
  70.   
  71.   <body>
  72.     
  73.     
  74.     <form action="" method="post">
  75.         <p>用户名:
  76.             <input type="text" name="name" id="name"> <input type="button" value="Check User Name" onclick="checkUserByAjax();">
  77.             
  78.         </p>
  79.         <div id="msg"></div>
  80.         <div id="statusMsg"></div>
  81.     </form>
  82.     
  83.     
  84.     
  85.   </body>
  86. </html>
阅读(363) | 评论(0) | 转发(0) |
0

上一篇:BOM及DOM加强

下一篇:extJS事件驱动

给主人留下些什么吧!~~