Thking In Java

          關注應用程序的HA、可擴展性 多實踐,遇到問題查doc,google,上論壇咨詢

           

          [導入]一個ajax例子

          上午比較閑,做了一個ajax的例子,功能是在一個表單中輸入用戶名,光標離開后,到服務器端判斷用戶輸入的名稱是否可用。
          1。頁面
           <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
          <title>注冊頁面</title>
          <script type="text/javascript">

          var xmlHttpRequest = null;

          function fValidUserName(){
              xmlHttpRequest = init();
              
              var url = "ValidUserName?userName="+document.getElementById("userName");
              xmlHttpRequest.open("Get", url, true);
              xmlHttpRequest.send(null);
              xmlHttpRequest.onreadystatechange=processRequest;    
          }

          function init(){
              if(window.XMLHttpRequest){
                  return new XMLHttpRequest();
              }else if(window.ActiveXObject){
                  return new ActiveXObject("Microsoft.XMLHTTP");
              }
          }
          function processRequest(){
              if(xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200){
                  processResponse();
              }
          }
          function processResponse(){
              var msg = xmlHttpRequest.responseXML;
              var text = msg.getElementsByTagName("Response")[0].firstChild.nodeValue;
              var span =    document.getElementById("validationMessage");
              var html;
              if(text == "valid"){
                  html = "您輸入的用戶名可以使用";
                  span.style.backgroundColor="#FFCC66";
              }else{
                  html = "您輸入的用戶名已經被使用";
                  span.style.backgroundColor="#FF0000";
              }
              span.innerHTML=html;
          }
          </script>
          </head>
          <body>
          <form action="register">
          <table>
              <tr><td>用戶名:</td><td><input type="text" name="userName" id="userName" onkeyup="fValidUserName()"><span id="validationMessage" style="background-color:ffcc66"></span></td></tr>
          </table>
          </form>
          </body>
          </html>

          2.服務器端:
           import java.io.IOException;
          import java.io.PrintWriter;
          import java.util.Random;

          import javax.servlet.ServletException;
          import javax.servlet.http.HttpServlet;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;

          public class ValidUserName extends HttpServlet {

               //to fix bug,原來沒有發送xml頭,導致一些不確定的因素發生。
               private final String XML_HEAD = "<?xml version=\"1.0\" encoding=\"gbk\"?>";
              /**
               * Constructor of the object.
               */
              public ValidUserName() {
                  super();
              }

              /**
               * Destruction of the servlet. <br>
               */
              public void destroy() {
                  super.destroy(); // Just puts "destroy" string in log
                  // Put your code here
              }

              /**
               * The doGet method of the servlet. <br>
               *
               * This method is called when a form has its tag value method equals to get.
               *
               * @param request the request send by the client to the server
               * @param response the response send by the server to the client
               * @throws ServletException if an error occurred
               * @throws IOException if an error occurred
               */
              public void doGet(HttpServletRequest request, HttpServletResponse response)
                      throws ServletException, IOException {

                  response.setContentType("text/xml");
                  response.setHeader("Cache-Control", "no-cache");
                  
                  PrintWriter out = response.getWriter();
                 out.print(XML_HEAD);//發送xml頭
                  Random random = new Random(System.currentTimeMillis());
                  if(random.nextBoolean()){
                      out.println("<Response>valid</Response>");
                      System.out.println("valid");
                  }else{
                      out.println("<Response>faild</Response>");
                      System.out.println("not valid");
                  }
                  out.flush();
                  out.close();
              }
          }



          文章來源:http://huxiaofei590.blog.163.com/blog/static/3259612200711612415966

          posted on 2007-12-06 13:24 ThinkInJava 閱讀(186) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章檔案

          java

          友情鏈接

          搜索

          最新評論

          主站蜘蛛池模板: 石阡县| 郯城县| 通州市| 台前县| 怀安县| 广水市| 宜宾市| 娄烦县| 海淀区| 日照市| 南江县| 兴仁县| 盐亭县| 东阿县| 泰来县| 海原县| 冷水江市| 夏津县| 稷山县| 嘉峪关市| 同心县| 丽水市| 麻栗坡县| 资兴市| 太谷县| 沅江市| 夏河县| 阜阳市| 云阳县| 滦平县| 平度市| 光山县| 达孜县| 澜沧| 分宜县| 年辖:市辖区| 太仓市| 陆丰市| 北流市| 宜阳县| 孝昌县|