• Post Reply Bookmark Topic Watch Topic
  • New Topic

getParameterValues problem  RSS feed

 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello everyone
i hope someone can help me with this

see, i have 2 jsps.
jsp1 passes an aray along with some other parameters like this

where vararray1 has been declared as


now in my jsp2.jsp, i do this


but what prints is this
[Ljava.lang.String

why is this so?
i want to get the values of the passed array from jsp1 to jsp2
what did i do wrong?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66158
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Is this actually the way you coded it?

If so, it doesn't jive with your results. With the above, the value of the parameter will be the string "valarray1".
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can only pass strings via the request parameters, not arrays.

You could put the array in session scope before redirecting to the second jsp file, with session.setAttribute("myarray", vararray1),
and retrieve it with String[] getarray = session.getAttribute("myarray");

Or you could put the array in request scope if you were forwarding the request to the second JSP.
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Satou kurinosuke:
You can only pass strings via the request parameters, not arrays.

You could put the array in session scope before redirecting to the second jsp file, with session.setAttribute("myarray", vararray1),
and retrieve it with String[] getarray = session.getAttribute("myarray");

Or you could put the array in request scope if you were forwarding the request to the second JSP.


oh. ok.
so i won't be able to pass arrays only strings with request parameters
so then, what's the request.getParameterValues for? won't that work with array passing between jsps?
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tried getting the size of vararray in jsp2 and it only outputs a size of 1
when in jsp1, there were 2 values in the array i passed.

so there has to be something wrong with the way i passed it?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66158
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bernard Sigmund Gustav:

so then, what's the request.getParameterValues for? won't that work with array passing between jsps?


No. First some terminology. You never pass anything from one JSP to another. You can share information via scoped variables, as Satou pointed out, or you can redirect to another JSP specifying request parameters on the query string. In either case you are not passing information in the technical sense of the word.

And as Satou pointed out, there is no way to pass anything but a string as a query parameter.

The getParameterValues() method is used to retrieve the values for a request parameter specified with multiple values. For example:



So you could use the above to send string array data to the second JSP by exploding it as above.

Now you still haven't answered my question. Why do your results in your original post not match your code?
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have no idea.
i actually passed or shared whatever, like this


and before that statement, i got the values of valarray from the database.
there are no problems with the data inside the array, just on how to pass it to the 2nd jsp

in the 2nd jsp, i did what i posted before


in jsp1, i printed the size of my array and it was 2
but in jsp2, the array size was only 1, plus what it outputs is this
[Ljava.lang.String
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66158
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


OK, that is not what you posted in your original post and it makes all the difference in the world. If you are looking for help it's important to be accurate and post the actual code. Otherwise you waste the time of people taking the time to try and help you.

And yes, the <%= %> construct will evaluate whatever expression it contains and convert it to a string. So if you place an array reference within it, you'll get the toString() equivalent of the array, which is just what you are getting.

So, for the 4th time or so, you cannot pass an array as a single query parameter. You'll either need to serilaize it across multiple query parameters as I showed if the array is small, and if it's a string array. Otherwise, you'll need to serialize the array into the hidden parameters of a form that you can post, or you'll need to use a scoped variable in the session to make the array available to another JSP.
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok. thanks.
so did the session thing like this
session.setAttribute("myarray",valarray);

and in jsp2, i got it with this
String[] myarray= session.getAttribute("myarray");

but i got this error
: incompatible types
found : java.lang.Object
required: java.lang.String[]
String[] myarray= session.getAttribute("myarray");
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not a compiler You could figure this out yourself :

 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh. ok.
thank you so much
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!