将javascript数组传递给servlet

我已经在SO上看过关于这个主题的先前问题,但我的问题还没有解决。

我将数组从javascript传递给servlet。

JavaScript代码:

var action = new Array(); function getProtAcionValues(rowNo,columnCount) { for(var j=0;j<columnCount;j++) { action[j] = document.getElementById('textActions'+rowNo+''+j).value; alert(action[j]); } } 

Servlet代码:

 String actions[] = request.getParameterValues("action[]"); if(actions!=null) for(int i=0;i<actions.length;i++) { System.out.print(" Action: "+actions); } else System.out.println("Action is null"); 

使用上面的代码我收到消息“Action is null”

如果我尝试

 String actions[] = request.getParameterNames("action[]"); 

我收到语法错误:

 The method getParameterNames() in the type ServletRequest is not applicable for the arguments (String) 

如果代码有问题,请告诉我。

你可以简单地使用数组的名称获取数组…

String actions [] = request.getParameterValues(“action”);

您不能将java数组作为参数传递,因为它是一个结构。 最好的方法是将它序列化为像jSon这样的字符串对象。 您可以使用JSON.stringify 。 简单高效。 您也可以在服务器中进行序列化,这非常有用。

使用表单操作传递Javascript数组变量以将值发送到servlet,然后使用

 String[] darray=request.getParameterValues("variable name used with link");