如何在jsp中获取数组值

时间:2014-08-26 06:41:59

标签: java javascript arrays jsp servlet-3.0

当我执行此操作时,当我在JSP文件中使用request.getParameter("quantity")时,只返回数组中的最后一个值作为输出。如何将整个数组及其所有值传递给JSP文件?

myfile1.js:

function savetheChanges()
{
    for(var i=0;i<16;i++)
    {
        quants[i]=document.getElementById("quantity"+i).value;
        kot1[i]=document.getElementById("kot"+i).value;
        itemcode1[i]=document.getElementById("itemcode"+i).value;
        var billno=document.getElementById("billno").value; 
        document.detailsview.action ="BillCB.jsp?method=" + "17" + "&billno=" +billno+ "&itemcode=" +itemcode[i]+ "&kot=" +kot1[i]+ "&quantity="+quants[i];
     }
}

myfile2.jsp:

(获取值时,我只得到数组的前两个元素)

billdetails_be.billno = Integer.valueOf(request.getParameter("billno"));
String[] quantCB = request.getParameterValues("quantity");
String[] kotCB = request.getParameterValues("kot");
String[] itemCB = request.getParameterValues("itemcode");
int[] quantarr = new int[quantCB.length];
int[] kotarr = new int[kotCB.length];
int[] itemarr = new int[itemCB.length];
System.out.println("beforeloop " + itemarr.length);
for (int i = 1; i < quantarr.length; i++) {
  quantarr[i] = Integer.parseInt(quantCB[i]);
}
for (int i = 1; i < kotarr.length; i++) {
  kotarr[i] = Integer.parseInt(kotCB[i]);
}
for (int i = 1; i < itemarr.length; i++) {
  itemarr[i] = Integer.parseInt(itemCB[i]);
}
for (int i = 1; i < itemarr.length; i++) {
  int quantint = quantarr[i];
  int kotint = kotarr[i];
  int itemint = itemarr[i];
  System.out.println("value in array:" + (quantarr[i]));
}

1 个答案:

答案 0 :(得分:0)

function savetheChanges()
{
    var quantity = new array[];
    for(var i=0;i<16;i++)
    {
        quantity[i]=document.getElementById("quantity"+i).value;
     }
     document.detailsview.action ="BillCB.jsp?method=" + "17" + "&billno=" +billno+ "&quantity="+quantity;
} 

一次在Query String中传递数组,然后

使用request.getParameterValues("quantity")它会给你数组

例如

<%

String[] favouriteCars = request.getParameterValues("favouritecar");
   for (int i = 0; i < favouriteCars .length; i++) {
      String car = favouriteCars [i];
      System.out.println("car name::"+car);

%>

上面的代码将获取所有汽车并将其打印在jsp上,或者您也可以使用JSTL在jsp上对您的数据进行操作