• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Passing array as hidden field

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic