The script code:
$(function () {
$(" # uname "). The onblur (function () {//when XXX loses focus
Var value=https://bbs.csdn.net/topics/$(" # uname "). Val ();//to get id for XXX element
$. Ajax ({
Url:/servlet/AjaxServlet,//to request the server url
Data: {value} "val" :,//it is an object, which represents the request parameter server can use request, getParamet () to obtain
If async: true,//for asynchronous request
Cache: false,//cache the results
Type: "POST",//request
DataType: "json",//what type the json data is returned from the server: receiving an object
Success: the function (result) {//this function can be carried on the server when success is called, parameters, the result is, the value of the server returns
If (result) {
Document. The getElementById (" notice "). The innerText="existing";
} else {
Document. The getElementById (" notice "). The innerText="hh";
}
}
});
});
});
The JSP code:
& lt; The form action="ZuCe_result. JSP method=" post "" name=" frm_register & gt;"User name: & lt;/td>
The servlet code:
@ WebServlet ("/AjaxServlet ")
Public class AjaxServlet extends the HttpServlet {
Private static final long serialVersionUID=1 l;
/* *
* @ see HttpServlet# HttpServlet ()
*/
Public AjaxServlet () {
Super ();
//TODO Auto - generated constructor stub
}
/* *
* @ see HttpServlet# doGet (it request, HttpServletResponse response)
*/
Protected void doGet (it request, HttpServletResponse response) throws ServletException, IOException {
//TODO Auto - generated method stub
request.setCharacterEncoding("utf-8");
The response. SetCharacterEncoding (" utf-8 ");
The String value=https://bbs.csdn.net/topics/request.getParameter (" val ");//in adjx data set
UserDao dao=new UserDao ();
Boolean rs=dao. IsExitUname (" value ");
If (rs) {
//use the jquery ajax callback function return value is the print the string
The response. GetWriter (). Print (" true ");
} else {
The response. GetWriter (). Print (" false ");
}
}
/* *
* @ see HttpServlet# doPost (it request, HttpServletResponse response)
*/
Protected void doPost (it request, HttpServletResponse response) throws ServletException, IOException {
//TODO Auto - generated method stub
DoGet (request, response);
}
}
CodePudding user response:
Is anyone here??CodePudding user response:
You need to have a problem, the url request path is/servlet/AjaxServlet in ajax, you have to @ WebServlet ("/AjaxServlet "), less a servlet, then test you can break point, if the request is coming after the background you can seeCodePudding user response: