//创建ajax请求对象 var xmlHttp; function createXMLHttpRequest(){ if(window.ActiveXObject){ xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } else if(window.XMLHttpRequest){ xmlHttp = new XMLHttpRequest(); } } /** * 表单提交 */ function submit(){ //发送请求 var searchContent = $id("search_input").value;//查找内容 createXMLHttpRequest(); try{ xmlHttp.onreadystatechange = handleStateChange; xmlHttp.open("GET", "/MyMap/QueryMapServlet?searchName="+searchContent, true); xmlHttp.send(null); }catch(exception){ alert("您要访问的资源不存在!"); } $("sideToggleRight").attr("checked","checked");//无作用 } //处理结果 function handleStateChange(){ if(xmlHttp.readyState == 4){ if (xmlHttp.status == 200 || xmlHttp.status == 0){ var t = xmlHttp.responseText; jsonobj = eval('('+t+')'); } } }
servlet端:
//oracle数据库查询的结果: Class.forName("oracle.jdbc.driver.OracleDriver"); conn = DriverManager.getConnection(url, "dio", "123"); stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); rs = stmt.executeQuery("SELECT * FROM GEO where ST like '%"+searchName+"%'"); ResultSetMetaData metaData = rs.getMetaData(); rs.beforeFirst();//将指针移到最开始的位置 int columnCount = metaData.getColumnCount(); //列数 JSONArray array = new JSONArray(); while (rs.next()) { JSONObject jsonObj = new JSONObject(); //输出数据 for (int i = 1; i <= columnCount; i++) { String columnName = metaData.getColumnLabel(i); String value = rs.getString(columnName); jsonObj.put(columnName, value); } array.put(jsonObj); } System.out.println(array); response.getWriter().print(array); }
更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript中ajax操作技巧总结》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》