Java-servlet request.getParameterValues ​​()

I have an array that contains other arrays that I pass as a parameter. I use request.getParameterValues()to get the parameter, but the problem is only that the original array is part of the array format. Arrays inside an array are converted to a string. Is there any other way to send and receive multidimensional arrays?

+5
source share
2 answers

If you use the GET method, you should build the request as follows:

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

If you use the POST method, you should use the application/x-www-form-urlencodedContent Type or just use the Post method in your HTML form. For instance:

 <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"]
+17

(,),

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

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

String[] ArrayList String[]

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

           String[] innerArray=outerArray[i].split(",");         
        }
+1

All Articles