我正在尝试使用Ajax将我的html文件中输入的输入数据发送到JSP。我能够从JSP文件接收数据并将其打印在我的HTML页面上,但是我无法将输入字段中输入的数据发送到JSP。
我在我的Ajax函数中设置了URL字段,但是当我在URL上使用JSP执行get参数时,它似乎不起作用。
String integerlist = request.getParameter("value1");
String nthnumber = request.getParameter("value2");
这是将数据发送到JSP页面的正确方法吗?
<HTML>
<HEAD>
<script type="text/javascript">
function loadXMLDoc() {
var value1= document.getElementById("list").value;
var value2= document.getElementById("nthnumber").value;
var url = 'solution.jsp?value1='+value1+'&value2='+value2;
var xmlhttp;
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
</script>
<TITLE> New Document </TITLE>
</HEAD>
<BODY>
<PRE>
<B>Enter List of Integers (Note that duplicates are allowed): </B> <input type="text" name="list" value="" id="list" /><br />
<B>Enter to find the N-th largest element in the supplied list:</B> <input type="text" name="nthnumber" value="" id="nthnumber" /><br />
<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<B><input type="submit" name="submit" value="Submit" onclick="loadXMLDoc()" > <input type="submit" name="clear" value=" Clear "></B>
</PRE>
</BODY>
</HTML>
另一个JSP文件:
<%@ page language="java" import="java.util.*" errorPage="" %>
<%@ page import="java.util.Set" %>
<%@ page import="java.util.TreeSet" %>
<%
String integerlist = request.getParameter("value1"); // requesting the list of integer values
String nthnumber = request.getParameter("value2"); // requesting the nth number
// String integerlist = "5,3,80,75,25,90,1";
int nthnumber = Integer.parseInt(nthnumber1);
String[] arraylist= integerlist.split(","); // splitting the requested string based on the delimiter
int[] results = new int[arraylist.length]; // creating integer array on the fly based on the arraylist received
for (int i = 0; i < arraylist.length; i++) {
results[i] = Integer.parseInt(arraylist[i]);
}
for (int i = 0; i < results.length; i++) {
for (int j = i+1; j < results.length; j++) {
if (results[i] < results[j]) {
int swap = results[i];
results[i] = results[j];
results[j]=swap;
}
if (results[i] == results[j]) {
results[j] = 0;
}
}
}
%>
<%=""+ results[nthnumber-1]%>