Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java-servlet request.getParameterValues()

I have an array which holds other arrays I am passing as a parameter. I am using request.getParameterValues() to get the parameter but the problem is only the original array is coming in the array format. The arrays inside the array are being converted to string. Is there another way to send and receive multi dimensional arrays?

like image 488
mayan Avatar asked Apr 11 '11 08:04

mayan


2 Answers

If you are using GET method you must build query like this:

http://localhost:8080/myApp/myServlet/?habits=Movies&habits=Writing&habits=Singing

If you are using POST method you must use application/x-www-form-urlencoded Content Type or just use Post method in your HTML form. For example:

 <form method="post">
 Habits :
    <input type="checkbox" name="habits" value="Reading">Reading
    <input type="checkbox" name="habits" value="Movies">Movies
    <input type="checkbox" name="habits" value="Writing">Writing
    <input type="checkbox" name="habits" value="Singing">Singing
    <input type="submit" value="Submit">
 </form>

Then in both cases in your servlet:

String[] outerArray=request.getParameterValues('habits');
your array will be filled with separated values:

//["Writing","Singing"]
like image 122
kospiotr Avatar answered Oct 23 '22 21:10

kospiotr


if the inner arrays are coming as comma(,) separated then try the below code

String[] outerArray=request.getParameterValues('parameterName');

String[] innerArray=outerArray[0].split(",");

Dynamically, you could do this and use different String[] to store the data or use an ArrayList of String[]

for (int i = 0; i < outerArray.length; i++) {

           String[] innerArray=outerArray[i].split(",");         
        }
like image 24
Sangeet Menon Avatar answered Oct 23 '22 21:10

Sangeet Menon