Ajax validates the database content and displays the value on the page

Time:2021-4-19

Function realization:

Fill in the content of the text box in the JSP page, leave the cursor from the text box, and get other data corresponding to the changed value in the database in the corresponding place of this page.

servlet:

request.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
//Call the servlet layer to search the database for the same user name and return to other records in the page
String client_id = request.getParameter("client_id");
ClientServices clientServices = new ClientServices();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Client client = clientServices.findClientById(client_id);
if (client != null) {
out.print(URLEncoder.encode(client.getClient_name(), "utf-8"));
} else {
out.print("false");
}
out.flush();
out.close();

jquery:

$(document).ready(function() {
$("#client_id").blur(function() {
$.ajax({
type : 'POST',
url : 'servlet/validServlet?client_id=' + $(this).val(),
data : 'client_id=' + $("#client_id").val(),
success : function(msg) {
if (msg == 'false') {
Alert ("no such person");
} else {
//UTF-8 decoding to solve Chinese garbled code
$("#clientInfo").html(decodeURI(msg));
$("#clientInfo").attr("value", decodeURI(msg));
}
}
});
});
});