ajax 调用onblur检查用户名是否存在时提示404 NOT FOUND错误
index.jsp// 首页,在这里输入用户名查询
<!doctype html>
<html lang="en">
<head>
<meta charset="gbk">
<title>Document</title>
</head>
<body>
<script>
window.onload = initPage;
function initPage() {
document.getElementById("userid").onblur = checkUserid;//引用函数方法
}
function createRequest() {
try {
request = new XMLHttpRequest();
} catch (tryMS) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (otherMS) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
request = null;
}
}
}
return request;
}
function checkUserid(){
request=createRequest();
var theName = document.getElementById("userid").value;
request.open("GET","CheckServlet?userid="+theName,true);
request.onreadystatechange=checkUseridCallback;
request.send(null);
document.getElementById("msg").innerHTML="正在验证...";
}
function checkUseridCallback(){
if(request.readyState==4){
if(request.status==200){
if( text=="true"){
flag=false;
document.getElementById("msg").innerHTML="用户ID重复";
}else{
flag=true;
document.getElementById("msg").innerHTML="此用户ID可以注册";
}
}
}
}
</script>
<form action="index.jsp" method="GET" ">
用户ID: <input type="text" id="userid" name="userid">
<span id="msg"></span>
<br>
姓名 <input type="text" name="name" >
</form>
</body>
</html>
package org.lxh.ajaxdemo;
import java.io.*;
import java.sql.*;
import
java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServlet;
public class CheckServlet extends HttpServlet{
public static final String DBDRIVER="com.mysql.jdbc.Driver";
public static final String DBURL="jdbc:mysql://localhost:8889/username?characterEncoding=gbk";
public static final String DBUSER="root";
public static final String DBPASS="root";
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws
ServletException,
IOException{
this.doPost(request, response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
request.setCharacterEncoding("gbk");