关闭

AJAX注册重名检测(绝对没有bug!)

标签: ajaximportexceptionxmlhttprequeststringdate
769人阅读 评论(0) 收藏 举报
分类:

将我以前写的AJAX代码贡献出来 

大家一起学习,绝对没有BUG!

共两个文件

ValidationServlet.java

validation.html

 //ValidationServlet.java

package ajaxbook.chap4;

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import bookshop.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ValidationServlet extends HttpServlet {   
   
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        PrintWriter out = response.getWriter();
       
        boolean passed = validateDate(request.getParameter("name"));
        response.setContentType("text/xml");
        response.setHeader("Cache-Control", "no-cache");
        String message = "Name Double!";
       
        if (passed) {
            message = "dgdf";
        }
        out.println("<response>");
        out.println("<passed>" + Boolean.toString(passed) + "</passed>");
  out.println("<message>" + message + "</message>");
        out.println("</response>");
        out.close();
     }
   
    /**
     * Checks to see whether the argument is a valid date.
     * A null date is considered invalid. This method
     * used the default data formatter and lenient
     * parsing.
     *
     * @param date a String representing the date to check
     * @return message a String represnting the outcome of the check
     */
    private boolean validateDate(String date){
       
        boolean isValid = true;
 String sql = "SELECT * FROM userpassword WHERE user_id='"+date+"';";
  DBSQLManager dbsm = new DBSQLManager();
  dbsm.setSqlStr(sql);

        try
        {

 dbsm.executeQuery();
 ResultSet rs =  dbsm.getRs();
 if (rs.next())
     {
            isValid = false;
     }

          }
        catch(Exception exception)
        {
            System.out.println(exception);
        };


        return isValid;
    }
}

 

//validation.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
  <head>
    <title>Using Ajax for validation</title>
 
    <script type="text/javascript">
        var xmlHttp;

        function createXMLHttpRequest() {
            if (window.ActiveXObject) {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            else if (window.XMLHttpRequest) {
                xmlHttp = new XMLHttpRequest();               
            }
        }

        function validate() {
            createXMLHttpRequest();
            var date = document.getElementById("name");
            var url = "ValidationServlet?name=" + escape(date.value);
            xmlHttp.open("GET", url, true);
            xmlHttp.onreadystatechange = callback;
            xmlHttp.send(null);
        }

        function callback() {
            if (xmlHttp.readyState == 4) {
                if (xmlHttp.status == 200) {
                    var mes = xmlHttp.responseXML.getElementsByTagName("message")[0].firstChild.data;
     var val = xmlHttp.responseXML.getElementsByTagName("passed")[0].firstChild.data;
                    setMessage(mes,val);
                }
            }
        }
       
        function setMessage(message,isValid) {           
            var messageArea = document.getElementById("dateMessage");
            var nameArea = document.getElementById("name");
            var fontColor = "red";
            if (isValid == "true") {
               messageArea.innerHTML = "";

            }
   else{
            messageArea.innerHTML = "<font color=" + fontColor + ">" + message + " </font>";
   nameArea.value="";
      nameArea.focus();
      }
        }

    </script>
  </head>
  <body>
    <h1>Enter your name</h1>
    Your name: <input type="text" size="10" id="name" onchange="validate();"/>
    <div id="dateMessage"></div>
  </body>
</html>

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:181716次
    • 积分:2575
    • 等级:
    • 排名:第14807名
    • 原创:43篇
    • 转载:150篇
    • 译文:0篇
    • 评论:48条
    文章分类
    最新评论