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

          友情鏈接

          搜索

          最新評論

          主站蜘蛛池模板: 景泰县| 马关县| 平安县| 临猗县| 屏东市| 开封县| 泾川县| 马尔康县| 克拉玛依市| 邵阳县| 弥渡县| 永靖县| 富平县| 乐都县| 开江县| 连州市| 文成县| 达拉特旗| 永嘉县| 晋州市| 临湘市| 大洼县| 合作市| 成武县| 宣化县| 云安县| 招远市| 桦南县| 临洮县| 达拉特旗| 远安县| 呈贡县| 鸡泽县| 江西省| 梓潼县| 定日县| 津南区| 温宿县| 广宗县| 大埔区| 沙湾县|