功能實現:
在jsp頁面中填寫文字框內容,游標離開文字框,在本頁面的相應地方擷取資料庫中改值所對應的其他資料。
servlet:
request.setCharacterEncoding("utf-8");PrintWriter out = response.getWriter();// 調用servlet層去資料庫尋找是否有相同使用者名稱 並返回到頁面中的其他記錄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("沒有此人");} else {//utf-8解碼解決中文亂碼$("#clientInfo").html(decodeURI(msg));$("#clientInfo").attr("value", decodeURI(msg));}}});});});