我有要求,即我需要将javascript数组传递给servlet。请指导我谢谢
qwe.js
<script type="text/javascript">
var array2 = [];
function getTotalTests() {
console.log("called");
console.log("called"+array1.length);
for (i=0; i < array1.length; i++) {
array2[i] = array1[i];
console.log(array2[i]);
}
};
</script>
我需要将array2传递给servlet
答案 0 :(得分:2)
您需要提出某种要求才能实现此目的。如果您不希望提出完整的请求,可以查看https://api.jquery.com/jQuery.ajax/以发出异步请求并显示所做的更改(如果需要)。
答案 1 :(得分:0)
你可以使用ajax传递
$.ajax({
type: "POST",
url: "servletname", //Your full URL goes here
data: {
dataname: datavalue
},
success: function (data, textStatus, jqXHR) {
},
error: function (jqXHR) {
}
});
答案 2 :(得分:0)
JSP页面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript">
function func()
{
var arr=[2,3,3];
var form = $('<form action="Test" method="get">' +
'<input type="hidden" name="id" value="'+arr+'">' +
'</form>');
alert( $(form));
$(form).submit();
}
</script>
<body>
<button onclick="func()">Deepak</button>
</body>
</html>
的Servlet
package test;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Test
*/
@WebServlet("/Test")
public class Test extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public Test() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println(request.getParameter("id"));
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}