Ajax js 使用Ajax檢測使用者名稱是否存在

來源:互聯網
上載者:User

jsp頁面: Js代碼

  1. <script type="text/javascript" language="javascript">   
  2.         var xmlHttp = false;   
  3.         try {   
  4.             xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");   
  5.         } catch (e) {   
  6.             try {   
  7.                 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");   
  8.             } catch (e2) {   
  9.                 xmlHttp = false;   
  10.             }   
  11.         }   
  12.         if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {   
  13.             xmlHttp = new XMLHttpRequest();   
  14.         }   
  15.            
  16.         //檢測使用者名稱是否存在   
  17.         function callServerCheckUname() {   
  18.             //通過GET方法請求,將請求封裝在URL中
      
  19.             var uname = document.getElementById("u_name").value;   
  20.             xmlHttp.open("POST", 'CheckUname?u_name='+uname);   
  21.             //設定伺服器響應的處理方法   
  22.             xmlHttp.onreadystatechange = updatePageCheckUname;   
  23.             // 發送請求   
  24.             xmlHttp.send(null);   
  25.         }   
  26.         function updatePageCheckUname() {   
  27.             if (xmlHttp.readyState == 4) {   
  28.                 var response = xmlHttp.responseText;   
  29.                 if(response >= 1){   
  30.                     alert("使用者名稱已存在!");   
  31.                 }else{   
  32.                     alert("可註冊!");   
  33.                 }   
  34.             }   
  35.         }   
  36.         </script>  
<script type="text/javascript" language="javascript">var xmlHttp = false;try {    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");} catch (e) {    try {        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");    } catch (e2) {        xmlHttp = false;    }}if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {    xmlHttp = new XMLHttpRequest();}//檢測使用者名稱是否存在function callServerCheckUname() {    //通過GET方法請求,將請求封裝在URL中    var uname = document.getElementById("u_name").value;    xmlHttp.open("POST", 'CheckUname?u_name='+uname);    //設定伺服器響應的處理方法    xmlHttp.onreadystatechange = updatePageCheckUname;    // 發送請求    xmlHttp.send(null);}function updatePageCheckUname() {    if (xmlHttp.readyState == 4) {        var response = xmlHttp.responseText;        if(response >= 1){        alert("使用者名稱已存在!");        }else{        alert("可註冊!");        }    }}</script>

web.xml設定檔: Xml代碼

  1. <servlet>  
  2.     <servlet-name>CheckUname</servlet-name>  
  3.     <servlet-class>servlet.AjaxCheckUname</servlet-class>  
  4.   </servlet>  
  5. <servlet-mapping>  
  6.     <servlet-name>CheckUname</servlet-name>  
  7.     <url-pattern>/CheckUname</url-pattern>  
  8.   </servlet-mapping>  
<servlet>    <servlet-name>CheckUname</servlet-name>    <servlet-class>servlet.AjaxCheckUname</servlet-class>  </servlet><servlet-mapping>    <servlet-name>CheckUname</servlet-name>    <url-pattern>/CheckUname</url-pattern>  </servlet-mapping>

servlet頁面: Java代碼

  1. public void doPost(HttpServletRequest request, HttpServletResponse response)   
  2.             throws ServletException, IOException {   
  3.         response.setContentType("text/html;charset=GB2312");   
  4.         PrintWriter out = response.getWriter();   
  5.         try {   
  6.             String uname = request.getParameter("u_name");   
  7.             RegistDAOImpl regDao = new RegistDAOImpl();   
  8.             out.print(regDao.checkUname(uname)) ;   
  9.         } catch (Exception e) {   
  10.         }   
  11.         out.flush();   
  12.         out.close();   
  13.     }  
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html;charset=GB2312");PrintWriter out = response.getWriter();try {String uname = request.getParameter("u_name");RegistDAOImpl regDao = new RegistDAOImpl();out.print(regDao.checkUname(uname)) ;} catch (Exception e) {}out.flush();out.close();}

DAO實作類別: Java代碼

  1. public int checkUname(String uname){   
  2.         Connection conn = null;   
  3.         PreparedStatement pstm = null;   
  4.         ResultSet rs = null;   
  5.         int success = 0;   
  6.         String sql = "select u_name from u_info where u_name='"+uname+"' or u_mobtel='"+uname+"' or u_email='"+uname+"'";
      
  7.         try {   
  8.             conn = getConnection();   
  9.             pstm = conn.prepareStatement(sql);   
  10.             rs = pstm.executeQuery(sql);   
  11.             if(rs.next()){   
  12.                 success = 1;   
  13.             }   
  14.             return success;   
  15.         } catch (Exception e) {   
  16.             e.printStackTrace();   
  17.         } finally {   
  18.             try {   
  19.                 conn.close();   
  20.             } catch (Exception e) {   
  21.                 e.printStackTrace();   
  22.             }   
  23.         }   
  24.         return success;   
  25.     }  
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.