在javascript中从java servlet读取数组 [英] Reading array from java servlet in javascript
问题描述
嗨喜欢标题说我试图从我的java Servlet中读取一个数组。我试图在我的java脚本文件中读取数组。
Hi like the title says I am trying to read an array from my java Servlet. I am trying to read the array in my java script file.
Java servlet代码:
String graphData [] = dbHandler.select(attributes); //用数据库中的数据填充数组。
request.setAttribute(graphData,graphData);
Java servlet code: String graphData[] = dbHandler.select(attributes); // filling the array with data from database. request.setAttribute("graphData", graphData);
RequestDispatcher dispatcher = request.getRequestDispatcher("/displayData.jsp");
dispatcher.forward(request, response);
<script type="text/javascript">
var graphData = ['${graphData}'];
var graphData= pageContext.getAttribute("graphData");
var graphData = document.getElementById("graphData");
var GraphData = ['${graphData}'];
log("test: " + graphData);
</script>
我尝试了所有这些选项,但都没有奏效。
有人可以告诉我从jsp页面中的java servlet读取数组的正确方法是什么?
I tried all those options but none of them worked. Can someone please tell me what the correct way is to read an array from a java servlet in a jsp page?
提前致谢!
编辑:
我能做的就是在JSP页面(在标题中)打印数组中的数据此代码:
what I can do is print out the data from the array on the JSP page (in the header) with this code:
<c:forEach items="${graphData}" var="temp">
<c:out value="${temp}"/><br />
</c:forEach>
但我想在JS代码中使用数组中的数据。由于某种原因不起作用。
But I want to use the data from the array in my JS code. which for some reason doesn't work.
推荐答案
你可以这样使用
<script>
var graphData = [
<c:forEach items="${graphData}" var="graph">
'<c:out value="${graph}" />',
</c:forEach>
];
console.log(graphData);
</script>
这篇关于在javascript中从java servlet读取数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!