【DRP】-servlet 请求传值

AJAX请求servlet ,动态验证分销商代码是否存在!

 

分销商文本框代码:

1 <input name="clientId" type="text" class="text1" id="clientId"
2         size="10" maxlength="10" onblur="validateClientId(this)">
3 <span id="spanClientId"></span>

 

创建jsp页面是可以使用模板:普通和高级。如果使用高级模板,会自动创建一下代码:

<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

html 代码<head>标签中会多出一行代码:

<base href="<%=basePath %>">

这行代码的作用是:把当前页面作为根页面(http://localhost:8080/drp3.1/),请求URL请求时资源是,直接写请求的页面就行无需在去..了!

例如下面的url请求就是直接写的资源路径。这样给开发带来很多方便。

 

validateClientID方法:

 1 <script type="text/javascript">
 2             function validateClientId(field) {
 3                 if (trim(field.value) != "") {
 4                     var xmlHttp = null;
 5                     //表示当前浏览器不是ie,如ns,firefox
 6                     if(window.XMLHttpRequest) {
 7                         xmlHttp = new XMLHttpRequest();
 8                     } else if (window.ActiveXObject) {
 9                         xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
10                     }
11                     var url = "servlet/ClientIdValidateServlet?clientId=" + trim(field.value);
12                     xmlHttp.open("GET", url, true);
13                     xmlHttp.onreadystatechange=function() {
14                         if (xmlHttp.readyState == 4) {
15                             if (xmlHttp.status == 200) {
16                                 if (trim(xmlHttp.responseText) != "") {
17                                     document.getElementById("spanClientId").innerHTML = "<font color='red'>" + xmlHttp.responseText + "</font>" 
18                                 }else {
19                                     document.getElementById("spanClientId").innerHTML = "";
20                                 }
21                             }else {
22                                 alert("请求失败,错误码=【" + xmlHttp.status + "】");
23                             }
24                         }
25                     };
26                     xmlHttp.send(null);
27                 }else {
28                     document.getElementById("spanClientId").innerHTML = "";
29                 }
30             }
31         </script>

 

通过配置文件寻找servlet 

<servlet>
        <servlet-name>ClientIdValidateServlet</servlet-name>
        <servlet-class>com.bjpowernode.drp.util.servlet.ClientIdValidateServlet</servlet-class>
        <load-on-startup>5</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>ClientIdValidateServlet</servlet-name>
        <url-pattern>/servlet/ClientIdValidateServlet</url-pattern>
    </servlet-mapping>

servelt请求创建的时期,默认是小于0的,如果是小于0在请求url时创建。如果如果大于0则是tomact启动时创建 。servlet只创建一次!

 

servlet请求代码:

 1 package com.bjpowernode.drp.util.servlet;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.ServletException;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 
10 import com.bjpowernode.drp.basedata.manager.ClientManager;
11 
12 /**
13  * 验证分销商代码是否存在
14  * @author Administrator
15  *
16  */
17 public class ClientIdValidateServlet extends HttpServlet {
18 
19     @Override
20     public void init() throws ServletException {
21         System.out.println("------------ClientIdValidateServlet.init()------------");
22     }
23 
24     @Override
25     protected void service(HttpServletRequest request, HttpServletResponse response)
26             throws ServletException, IOException {
27         response.setContentType("text/html;charset=GB18030");
28         String clientId = request.getParameter("clientId");
29         boolean flag = ClientManager.getInstance().findClientByClientId(clientId);
30         if (flag) {
31             response.getWriter().print("分销商代码已经存在");
32         }
33     }
34 
35     
36 }

后台D层方法方法:

 1 /**
 2      * 根据分销商代码查询
 3      * @param clientId
 4      * @return 存在返回true,否则返回false
 5      */
 6     public boolean findClientByClientId(String clientId) {
 7         String sql = "select count(*) from t_client where client_id=?";
 8         Connection conn = null;
 9         PreparedStatement pstmt = null;
10         ResultSet rs = null;
11         boolean flag = false;
12         try {
13             conn = DbUtil.getConnection();
14             pstmt = conn.prepareStatement(sql);
15             pstmt.setString(1, clientId);
16             rs = pstmt.executeQuery();
17             rs.next();
18             return rs.getInt(1) > 0 ? true : false;
19         }catch(SQLException e) {
20             e.printStackTrace();
21         }finally {
22             DbUtil.close(rs);
23             DbUtil.close(pstmt);
24             DbUtil.close(conn);
25         }
26         return flag;
27     }

一篇关于没有用servlet 的AJAX请求博客:

【DRP】AJAX异步判断用户是否存在

 

输出提示的方式比较:

response.getWriter().write()与out.print()的区别

编程小技巧:

  使用jsp高级模板,无需因为资源的路径而烦恼!

总结:不断提高自己的编码效率!

posted @ 2016-07-03 20:29  像大海一样生活  阅读(186)  评论(0编辑  收藏  举报