[英]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.