简体   繁体   English

将javascript数组传递给servlet

[英]Passing javascript array to servlet

I have looked previous questions on this topic on SO, but my problem is not solved yet. 我已经在SO上看过关于这个主题的先前问题,但我的问题还没有解决。

I am passing the array from javascript to servlet. 我将数组从javascript传递给servlet。

JavaScript Code: 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 Code: 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");

Using above code I am getting message "Action is null" . 使用上面的代码我收到消息“Action is null”

And if I try 如果我试试

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

I am getting Syntax error: 我收到语法错误:

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

Please let me know if there is something wrong in code. 如果代码有问题,请告诉我。

you can just simply get the array with the name of the array... 你可以简单地使用数组的名称获取数组...

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

You can't pass a java array as a parameter, as it is an structure. 您不能将java数组作为参数传递,因为它是一个结构。 The best way is to serialize it into an string object like a jSon. 最好的方法是将它序列化为像jSon这样的字符串对象。 You can use JSON.stringify . 您可以使用JSON.stringify Simple and efficient. 简单高效。 As you can serialize in the server also, it's very useful. 您也可以在服务器中进行序列化,这非常有用。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM