JS请求servlet功能示例
沧-浪-之 人气:0本文实例讲述了JS请求servlet的方法。分享给大家供大家参考,具体如下:
前端js代码:
//创建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程序设计有所帮助。
加载全部内容