<script type="text/javascript"> function judgeLandCode(){ console.log(1) $.ajax({ url: "checkusername.do",//获取地址栏 //传递给后台的参数 : 输入框框的参数 data: {"usercode": $("#username").val()}, success: function (data) {//后台返回的参数data //如果后台data的参数和输入框的参数一样的话 console.log(data) if (data=='yes'){ //如果返回yes,就代表用户名能用 // $('#nameTipMsg').css("color","green"); $('#nameTipMsg').css('color','green'); }else{ $('#nameTipMsg').css("color","red") } $('#nameTipMsg').html(data); } }); } </script> 用户名:<input type="text" id="username" onblur="judgeLandCode()"/> <span id="nameTipMsg" style="font-size: 12px;"></span><br>
serlvet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("utf-8"); response.setContentType("text/html; charset=UTF-8" ); PrintWriter out = response.getWriter(); //获取前台的参数 String usercode = request.getParameter("usercode");
UserService userService = new UserServicelmpl();调用service业务层判断数据数据
//这里已经把登陆了的人查出来了
User user = userServlet.Login(usercode);//把前端的参数放到
//判斷數據庫裏面有沒有你輸入的這個用戶名,如果有就返回不可用,乳溝沒有就防護顯示可用
//数据库的用户名和你数据的不等于null 就有这个返回不可用
if (user!=null){
out.print("no");
}else{
out.print("yes");
}
out.close();
}