简体   繁体   中英

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?

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

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(",");         
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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