• Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing array as hidden field  RSS feed

 
Ketan Mohite
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have 2 JSP pages.I want to pass an array from one JSP page to another.How should I proceed ?

e.g.
1.source.jsp
//
String locations[]={"Mumbai","Newyork","London"}
<input type="hidden" name ="loc" value=............>
//

2.target.jsp
//
String locations[] =request.getParameter(..............);
//

Thanks & Regards,
Ketan
 
Manesh Kumar
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have 2 JSP pages.I want to pass an array from one JSP page to another.How should I proceed ?


You can form multiple hidden fields with same name and

use function call


or

You can form comma seperated string for the array and then print it to hidden field.
And while submission, you can again tokenize it.

I feel first would be better approach.

Hope this works.
 
Jass Singh
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume that JSP1 is painted fully on browser & then on submit, control goes to JSP2. Well you can do it in 2 ways;

1. Iterate over array & concatenate all its elements using some separator e.g. ~. Then set it as value of a hidden field in JSP1 say arrayfield.



Then in JSP2 retrive it using & using StringTokenizer retrive array elements from it.

2. Though not advisable but, set array in session using in JSP1 & in JSP2 retrive using Remember to remove it from session in JSP2 if you dont need it later in session.

However if JSP1 is just an intermediate jsp & forwards the request to JSP2 using <jsp:forward.. then you can set array as a request attribute in JSP1 and retrive it in JSP using

Hope that helps.
 
Satish SN
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I go with Dilip's Idea of having multiple hidden values with the same name and u can retrieve the same on other page by
String[] stre = request.getParameterValues("");

this is very efficient way of doing things
 
Ketan Mohite
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies....

Actually I was knowing,how to retrieve multiple values...using reqest.getParameterValues(...).

I had a problem in sending array as hidden parameter..

By the way, I solved my problem using for loop...I did something like this.....

1.source.jsp
//
<%String locations[]={"Mumbai","Newyork","London"};
for(int i=0;i<locations.length;i++){
%>
<input type="hidden" name ="loc" value="<%=locations[i]%>">
<%
}
%>

2.target.jsp
String locations[] =request.getParameter(loc);

Thanks & Regards,
Ketan
 
Ketan Mohite
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually the last line should be
String locations[] =request.getParameterValues(loc);

Thanks & Regards,
Ketan
 
Jass Singh
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by kumar satish:
I go with Dilip's Idea of having multiple hidden values with the same name and u can retrieve the same on other page by
String[] stre = request.getParameterValues("");

this is very efficient way of doing things


Dilip posted reply while i was typing mine. Yes, indeed its a better approach, completely missed by me.

Regards
Jass
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!