We help IT Professionals succeed at work.

request.getParameterValues()---how?

boyyle
boyyle asked
on
i have this code in my jsp page:

<%
String myArray[] = request.getParameterValues("myTextBox");
int myLength = java.lang.reflect.Array.getLength(myArray);
%>

so,my concern is, if myArray[] not empty,the java.lang.reflect.Array.getLength is working.
but if myArray[] is empty, i get the error message....
so, may i know is there any function to check whether the myArray[] is empty or not???

thanx
Comment
Watch Question

Commented:
Check if it is null.

Commented:
To check the length of array, you can use
    myArray.length

This will return 0 if the myArray empty.int myLength = (myArray == null ? 0 : myArray.length);
Ovi

Commented:
myArray.length
Commented:
request.getParameterValues("myTextBox");
return null if "myTextBox" has no values, otherwise, it return array of String.

You may check returned value if it is empty or not as following:

<%
String myArray[] = request.getParameterValues("myTextBox");
int myLength;
if (myArray != null) myLength = myArray.length;
%>

when you declare a array variable, that variable has the attribute length to determine the number of elements of array.

array variable is a object, so you can check it empty or not by compare it to null value

best regards
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
I believe Array.length(array) returns the same thing as array.length.
SO I'd say the problem is that you need to check if null as yongsing originally stated.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.