简体   繁体   English

java-servlet request.getParameterValues()

[英]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. 我使用request.getParameterValues()来获取参数,但问题是只有原始数组以数组格式出现。 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: 如果您使用的是GET方法,则必须构建如下查询:

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. 如果您使用的是POST方法,则必须使用application/x-www-form-urlencoded内容类型,或者只使用HTML表单中的Post方法。 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: 然后在你的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[] 动态地,您可以执行此操作并使用不同的String[]来存储数据或使用String[]ArrayList

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 servlet request.getParameterValues(fieldName)返回null并引发异常 - servlet request.getParameterValues(fieldName) returns null and throw exception 如何使用request.getParameterValues? - How to use request.getParameterValues? 在jsp页面中request.getParameterValues获取空值 - request.getParameterValues getting null in jsp page 将request.getParameterValues()转换为int数组 - Converting request.getParameterValues() to int array 如果不在IE5怪癖模式下,则Request.getParameterValues返回null - Request.getParameterValues returns null when not in IE5 quirks mode 如何将request.getParameterValues与mybatis for List匹配? 串[]? 或清单 <String> ? - How do I match request.getParameterValues with mybatis for List? String[]? or List<String>? 编译 java 文件和 java-servlet 文件时出错 - Error while compiling java files and java-servlet files Java-Servlet生成的CSV:编码始终为“未定义” - CSV generated by Java-Servlet: Encoding always “undefined” 在Servlet中使用getParameterValues时的ArrayIndexOutOfBoundsException - ArrayIndexOutOfBoundsException when use getParameterValues in Servlet 使用getParameterValues将日期从jsp传递到servlet - passing date from jsp to servlet using getParameterValues
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM