• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

passing array parameter in URL

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Is it possible to pass an array object in URL? something like..

var emp = new Array();

emp[0] = "Emp1";
emp[1] = "Emp1";
emp[2] = "Emp1";
emp[3] = "Emp1";

then pass using

http://localhost:8080/servlet/MyServlet?empArray=emp;

then in jsp,

request.getParameterValues("emp");

Please advise.
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is more a html/javascript question than JSP.
In order for request.getParameterValues("emp") to work, you have to pass along a url string something like
myPage.jsp?emp=emp1&emp=emp2&emp=emp3

Notice that the parameter emp appears multiple times, each time with a new value.
You can construct this string yourself in javascript with a simple loop, and navigate to that url.


Alternatively you can use the toString method of an array to turn the values into a comma separated list, and send that as a single parameter.
At the JSP end you would retrieve the parameter, and then use the String.split method to split it on commas, and turn it into a java array.

Whichever approach you use, you should probably use the javascript function window.escape() to escape any characters that might be affected by putting them in a url string.

Good luck,
evnafets
 
Marshal
Posts: 67418
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the array is any size at all you will quickly run out of room in the URL, so this is a very poor way to try and pass anything but small amounts of data.

What are you really trying to accomplish?
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
simply you should retrieve that values by using Enumeration enum1=request.getParameterNames();
while(enumm.hasMoreElements())
{
str=(String)enumm.nextElement();
if (str.indexOf("emp"))>=0)
value=request.getParameter(str);
}

by using this u will receive all emp relative parameters and values.
 
Richard Mendoza
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stefan Evans,

That really helps me a lot! Thanks for help!
 
It's hard to fight evil. The little things, like a nice sandwich, really helps. Right tiny ad?
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic