servlet:checkName
request.setCharacterEncoding("UTF-8")
String name=request.getParameter("name")
name = new String(name.getBytes("ISO-8859-1"),"UTF-8")
User user=new UserDao().checkName(name)
PrintWriter out=response.getWriter()
if(user==null){
out.print("true")
out.flush()
out.close()
}else{
out.print("false")
out.flush()
out.close()
JS文件
$("[name=addname]").blur(function (){
var value=this.value;
var obj=$(this);
$.ajax({
url:"checkName",
type:"post",
data:{"name":value},
dataType:"text",
success:function(type){
$(".message").remove();
if("true"==type){
obj.after("<span class='message greenMeg'>可以使用</span>");
$("#add").removeAttr("disabled");
}else{
obj.after("<span class='message redMeg'>已被使用</span>");
$("#add").attr("disabled", "disabled");
}
}
});
});