功能實現:
在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)); } } }); }); });