在我的 java 项目中,我正在使用 jsp 页面。这里我只传递一个参数,我想从数据库中检索多个(3)值。我知道如何只传递和获取单个参数。但我不知道如何通过传递单个参数来获取多个值。我的ajax代码如下:
var request;
function sendInfo1() {
var v = document.Example.wayid.value;
// var url = 'Showroutelink.jsp?val='+v+'&start='+v1+'&end='+v2;
var url = 'Showroutelink.jsp?val='+v;
if (window.XMLHttpRequest) {
request = new XMLHttpRequest();
} else if (window.ActiveXObject) {
request = new ActiveXObject("Microsoft.XMLHTTP");
}
try {
request.onreadystatechange = getInfo1;
request.open("GET", url, true);
request.send();
} catch (e) {
alert("Unable to connect to server");
}
}
function getInfo1() {
if (request.readyState == 4) {
var val = request.responseText.split('|');
document.getElementById("start").innerHTML = val[0];
document.getElementById("end").innerHTML = val[1];
document.getElementById("waypoints").innerHTML = val[2];
}
}
This is My Setver side Java code
<%
String R_Name=request.getParameter("val");
int w_id=0;
String wplist = "";
// String R_Name = request.getParameter("way");
try {
String buffer = "<select name='state'>";
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost/googlemap", "root",
"root");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select start from route where Route_Name='" + R_Name + "'");
while (rs.next()) {
String Startval = rs.getString("start");
buffer = buffer + "<option value='" + Startval + "'>" + Startval + "</option>";
}
buffer = buffer + "</select>";
response.getWriter().println(buffer);
} catch (Exception e) {
}
try {
String buffer1 = "<select name='state'>";
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost/googlemap", "root",
"root");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select end from route where Route_Name='" + R_Name + "'");
while (rs.next()) {
String Endval = rs.getString("end");
buffer1 = buffer1 + "<option value='" + Endval + "'>" + Endval + "</option>";
}
buffer1 = buffer1 + "</select>";
response.getWriter().println(buffer1);
} catch (Exception e) {
}
try {
String buffer2 = "<select name='state'>";
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection con1 = DriverManager.getConnection("jdbc:mysql://localhost/googlemap", "root",
"root");
Statement st1 = con1.createStatement();
ResultSet rs1 = st1.executeQuery("select wplist from route where Route_Name='" + R_Name + "'");
while (rs1.next()) {
wplist = rs1.getString("wplist");
String wplist1[] = wplist.split("/");
buffer2 = buffer2 + "<option value='" + wplist1 + "'>" + wplist1 + "</option>";
}
buffer2 = buffer2 + "</select>";
response.getWriter().println(buffer2);
} catch (Exception e) {
}
%>
答案 0 :(得分:1)
如果我的问题是正确的,那么您就是在询问如何在请求中添加多个查询参数。
查询参数结构如下:
Showroutelink.jsp?key_1=value_1&key_2=value_2&key_n=value_n
所以你将遵循这种模式。
如果您询问如何从服务器调用返回多个值,请尝试使用JSON。
答案 1 :(得分:0)
如果您的ajax调用返回的数据是JSON字符串,即(responseText = '{ "name":"John", "age":30, "city":"New York"}'
),那么您可以使用以下内容:
function getInfo1() {
if (request.readyState == 4) {
var val = request.responseText;
var obj = JSON.parse(request.responseText);
document.getElementById("start").innerHTML = obj.start;
document.getElementById("end").innerHTML = obj.end;
document.getElementById("waypoints").innerHTML = obj.waypoints;
}
}
如果您能提供ajax调用返回的响应,我可以更好地回答您的问题。
答案 2 :(得分:0)
我没有完全理解这个问题,因为你还没有分享你的java代码。但在JS中你总是可以创建一个数组并将其分配给url。 var v = [para1,para2,para3];
我认为这应该有效。 创建一个数组。
var v= [];
然后添加所有参数
v.push(document.Example.wayid1.value);
v.push(document.Example.wayid2.value);
v.push(document.Example.wayid3.value);
然后像这样添加到URL。
var url = 'Showroutelink.jsp?val=' + v.toString();
在java端你可以得到。
String[] routeNames=request.getParameter("val");
\\in case error occurs use
String[] routeNames=request.getParameter("val").split(",");
还有一件事,不是使用带有“=”的where子句,而是使用“in”,只是循环遍历结果集。