AJAX验证数据库内容并显示在页面

功能实现:

在jsp页面中填写文本框内容,光标离开文本框,在本页面的相应地方获取数据库中改值所对应的其他数据。

servlet:

 1      request.setCharacterEncoding("utf-8");
 2         PrintWriter out = response.getWriter();
 3         // 调用servlet层去数据库查找是否有相同用户名 并返回到页面中的其他记录
 4         String client_id = request.getParameter("client_id");
 5         ClientServices clientServices = new ClientServices();
 6         try {
 7             Thread.sleep(2000);
 8         } catch (InterruptedException e) {
 9             e.printStackTrace();
10         }
11         Client client = clientServices.findClientById(client_id);
12         if (client != null) {
13             out.print(URLEncoder.encode(client.getClient_name(), "utf-8"));
14         } else {
15             out.print("false");
16         }
17         out.flush();
18         out.close();

 

jquery:

 1 $(document).ready(function() {
 2         $("#client_id").blur(function() {
 3             $.ajax({
 4                 type : 'POST',
 5                 url : 'servlet/validServlet?client_id=' + $(this).val(),
 6                 data : 'client_id=' + $("#client_id").val(),
 7                 success : function(msg) {
 8                     if (msg == 'false') {
 9                         alert("没有此人");
10                     } else {
11                         //utf-8解码解决中文乱码
12                         $("#clientInfo").html(decodeURI(msg));
13                         $("#clientInfo").attr("value", decodeURI(msg));
14                     }
15                 }
16             });
17         });
18     });

 

posted @ 2014-08-22 20:45  最重要的小事  阅读(1927)  评论(0编辑  收藏  举报