viki viki - 7 months ago 30
Java Question

how to get the array values in jsp

When I execute this only the last value in array returned as output when I use

request.getParameter("quantity")
in my JSP file. How can I pass the entire array with all of its values to the JSP file?

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:

(While getting the values I get only the first two elements of array)

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]));
}

Answer
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;
} 

pass array in Query String once and then

use request.getParameterValues("quantity") it will give you array

for example

<%

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

%>

above code will get all cars and print it on jsp or you can also use JSTL to itrate your array on jsp