日期:2014-05-20  浏览次数:20840 次

ajax实现无刷新检测
JScript code

<script language="javascript">
var http_request = false;
function createRequest(url) {
    //初始化对象并发出XMLHttpRequest请求
    http_request = false;
    if (window.XMLHttpRequest) { // Mozilla或其他除IE以外的浏览器
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType("text/xml");
        }
    } else if (window.ActiveXObject) { // IE浏览器
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");

           } catch (e) {}
        }
    }
    if (!http_request) {
        alert("不能创建XMLHTTP实例!");
        return false;
    }
    http_request.onreadystatechange = alertContents;    //指定响应方法
    //发出HTTP请求
    http_request.open("GET", url, true);
    http_request.send(null);
}
function alertContents() {    //处理服务器返回的信息
    if (http_request.readyState == 4) {
        if (http_request.status == 200||http_request.status==0) {
            alert(http_request.responseText);
        } else {
            alert('您请求的页面发生错误!');
        }
    }

}
function checkName() {
    var username = form1.username.value;
    if(username=="") {
        window.alert("请添写用户名!");
        form1.username.focus();
        return false;
    }
    else {
        createRequest('checkName.jsp?username='+username);
    }
}
</script>


HTML code
 <td width="293" id="td1"><input type="text" id="username" name="username"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" value="检测用户名" onClick="checkName()"></td>

通过Onclick调用函数,转入checkName.jsp文件,如下:
SQL code

<%//request.setCharacterEncoding("GB2312");
String username=request.getParameter("username");
try{
            Class.forName("com.mysql.jdbc.Driver");
        }catch (ClassNotFoundException e) {
            
            e.printStackTrace();
        }
    //    try{
            Connection conn=DriverManager.getConnection("jdbc:mysql://localhost/hrm","root","root");
    /*    }catch (SQLException e) {
            
            e.printStackTrace();
        }*/
Statement stmt=conn.createStatement();
String sql="select * from userinfo  where userName='"+username+"'";
ResultSet rs=stmt.executeQuery(sql);
if (rs.next()){
    out.println("很报歉!用户名"+username+"已经被注册!");
}else{
    out.println("祝贺您!用户名"+username+"没有被注册!");
}
rs.close();
conn.close();
stmt.close();%>


为什么不管我输入什么,都提示没有被注册,我输入的东西数据库有,我用servlet检测时就是正确的,但是用这个不知道为什么不对

------解决方案--------------------
你在jsp里面打印一个username 很有可能是一个空串 你的createRequest传入的url不要在括号里面做拼接 拼接好再传进去
------解决方案--------------------
把SQL打印出來,然后丢到数据库执行
------解决方案--------------------
你应该先按照1楼说的 先打印 String username=request.getParameter("username");
这个 username 有可能是空值 还有可能是乱码 如果都不是那你就 打印sql语句 到数据库执行下就知道是代码的问题还是数据哭的问题了 如果数据库的sql是对的 那就可能是链接数据库时候的编码跟数据库不一致导致查询出错 你可以用characterEncoding=数据库编码 eg: con = DriverManager.getConnection("jdbc:mysql:///db?characterEncoding=utf-8", "root", "123456"); 先找到问题的所在 希望对你有帮住
------解决方案--------------------