java-servlet request.getParameterValues()

may*_*yan 5 java arrays jsp servlets

我有一个数组,其中包含我作为参数传递的其他数组.我request.getParameterValues()用来获取参数,但问题是只有原始数组以数组格式出现.数组中的数组正在转换为字符串.还有另一种发送和接收多维数组的方法吗?

kos*_*otr 21

如果您使用的是GET方法,则必须构建如下查询:

http://localhost:8080/myApp/myServlet/?habits=Movies&habits=Writing&habits=Singing
Run Code Online (Sandbox Code Playgroud)

如果您使用的是POST方法,则必须使用application/x-www-form-urlencoded内容类型,或者只在HTML表单中使用Post方法.例如:

 <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>
Run Code Online (Sandbox Code Playgroud)

然后在你的servlet的两种情况下:

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

//["Writing","Singing"]
Run Code Online (Sandbox Code Playgroud)


San*_*non 2

如果内部数组以逗号(,)分隔,则尝试以下代码

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

String[] innerArray=outerArray[0].split(",");
Run Code Online (Sandbox Code Playgroud)

动态地,您可以执行此操作并使用不同的String[]数据来存储数据或使用ArrayListofString[]

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

           String[] innerArray=outerArray[i].split(",");         
        }
Run Code Online (Sandbox Code Playgroud)