Enter an ID in the table, and then automatically search for the corresponding name in the database based on the ID
This is the Javascript part, which is verified by Ajax.
$ (Document ). ready (function () {$ ("# client_id "). blur (function () {$. ajax ({type: 'post', URL: 'servlet/valclientservlet ', // This is the background servlet method data: 'client _ id = '+ $ ("# client_id "). val (), // client_id is the ID attribute beforesend: function () {$ ("# progress" ).html ("AAA") ;}, complete: function (XMLHttpRequest, textstatus) {// This is a short sleep before execution. The method is implemented in servlet $ ("# progress" ).html ("") ;}, success: function (MSG) {If (MSG = 'false') {// if it is false, the alert pop-up box prompts No, and make the DIV with the displayed name empty alert ("No such person"); $ ("# client_name" ).html ("");} else {// judge as true, display the name value in the Div. decodeuri () is to prevent garbled $ ("# client_name" ).html (decodeuri (MSG ));}}});});});
This is servlet content
Public void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {response. setcontenttype ("text/html"); printwriter out = response. getwriter (); Request. setcharacterencoding ("UTF-8"); response. setcharacterencoding ("UTF-8"); string client_id = request. getparameter ("client_id"); try {thread. sleep (1000); // This row is the sleep before execution, and the number in the brackets is in milliseconds} catch (interruptedexc Eption e) {// todo auto-generated Catch Block E. printstacktrace ();} // call services to go to the database and check whether the same user name backdao = new backdao (); e_client eclient = Dao. findbyid (client_id); // The findbyid () method is a method that contains SQL query statements and queries the database // Boolean isselect = Dao. selectclient (client_id); If (eclient! = NULL) {out. print (urlencoder. encode (eclient. getclient_name (), "UTF-8"); // decodeuri () method that prevents garbled characters in Ajax. You need to add} else {out. print (false);} Out. flush (); out. close ();}