File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Retrieve array parameters from a request Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Retrieve array parameters from a request" Watch "Retrieve array parameters from a request" New topic
Author

Retrieve array parameters from a request

Hikari Shidou
Ranch Hand

Joined: Jan 22, 2013
Posts: 88
Hello guys. Many years ago I used to develop in Java, then I moved to PHP because I started working in Wordpress, and now I'm slowly moving back to Java.

During these years I improved a lot as profession, and learned a lot in PHP. Now my Java skills are outdated and there are stuff I'm able to do in PHP and I don't know how to in Java.

In HTML we can put request parameters in array, and easily retrieve them in PHP, as follows

HTML form:


Then we retrieve in PHP:


That's a lot better than having a lot of separated variables in a request. Specially when we use krumo to dump them, it gets way easier to see what's received.

I wanna use the same structure in Java Servlet, but I have no idea how to get data receive in that structure.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61752
    
  67

Simply give all of the elements the same name, then use getParameterValues() to retrieve them as an array.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61752
    
  67

Hikari Shidou wrote:In HTML we can put request parameters in array

Not really. HTML has no concept of arrays. The content of the name attribute is just a name. Using something that looks like array notation is irrelevant to the HTML, as well as to HTTP.

PHP simply assigns semantics to names of that format. HTML and HTTP do not have anything to do with it.
Hikari Shidou
Ranch Hand

Joined: Jan 22, 2013
Posts: 88
Bear Bibeault wrote:Simply give all of the elements the same name, then use getParameterValues() to retrieve them as an array.


But then how will I get to know what each value refers to? In PHP each value is related to a index inside the "array" (yes I know it's an ordenered map and not an actual array, but we get used to call it array :p )

Bear Bibeault wrote:Not really. HTML has no concept of arrays. The content of the name attribute is just a name. Using something that looks like array notation is irrelevant to the HTML, as well as to HTTP.

PHP simply assigns semantics to names of that format. HTML and HTTP do not have anything to do with it.


Interesting... so that's how it is? So the ['s and ]'s are just chars as any other on the name of the parameter, and PHP does the trick merging many parameters into an array?

For now I only have Java+Servlet development environment at work, that's why I asked without testing first. Indeed, I'll have to find a tutorial to remember how to create basic Servlets and how to retrive GET and POST parameters. After all these years I forgot lol
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61752
    
  67

Hikari Shidou wrote:But then how will I get to know what each value refers to?

Not sure what you are asking here. You'll get a String[] that you can index as usual with [0], [1], and so on.

Or are you actually not looking to populate an array, but rather a bean with properties? If so, BeanUtils.populate() is useful. (BeanUtils is an Apache project.)

Interesting... so that's how it is? So the ['s and ]'s are just chars as any other on the name of the parameter, and PHP does the trick merging many parameters into an array?

Yes.
shivam singhal
Ranch Hand

Joined: Jul 15, 2012
Posts: 226

request.getParameter("name_of_the_variable");

you must give the "method name" by which your HTML send data to the acttion SERVLET.. as servlet service method will use the same METHOD(doGet , doPost , doHead , etc)
by default its doGet
shivam singhal
Ranch Hand

Joined: Jul 15, 2012
Posts: 226

ABOVE :: request is of HttpServletRequest type..
shivam singhal
Ranch Hand

Joined: Jul 15, 2012
Posts: 226

if you want to retrieve array parameters the you can use

request.getParameterValues("name")[0];
arkaes Duraimoni
Greenhorn

Joined: Jan 23, 2013
Posts: 14
WE can do this by both ways
in jsp
<form>
<%for (int i=0;i<10;i++)%>
<input type="text" name="mainPatameter<%=i%>" />

<% }%>
</form>
now your name has different values . so that in servlet you can get the values very easily

int totalRecords = 10;/if you need to get dynamically just set one hidden field in jsp and get it or keep it in request
String arr[] = new String[totalRecords ]
for(int i=0;i<totalRecods;i++){
arr[i] =request.getParameter("mainPatameter"+i);
}
//now your arr[] having your dynamic values.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61752
    
  67

All that adding of suffixes is needless. Again, just give the elements the same name and retrieve them as an array with getParameterValues(). Why add all that unnecessary complexity?
Hikari Shidou
Ranch Hand

Joined: Jan 22, 2013
Posts: 88
Thanks guys!

I'm really not used anymore to Java and Servlet lol, I think PHP now. I'm gonna take a look on BeanUtils.populate()

I think we are diverging on the meaning of array, sorry for that. Who doesn't know PHP may not know how array is implemented in it. It's indeed an ordered map, with keys that may be ints or strings.

So, if I have HTML inputs with these names: param[key1], param[key2], param[key3], I just do in PHP $param = $_POST['param'], and all those 3 parameters are gathered together, then I can for exemple do $var = $param['key1']
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61752
    
  67

Yeah, that is not an array -- I've heard it called an "associative array" but that's really really outdated term. The Java equivalent is a Map.

JavaScript objects have similar capabilities.

If you are going to be working in servlets and Java, I'd make an effort to ditch the PHP baggage. It's just gonna lead you to some bad places.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61752
    
  67

P.S. If all you want is a Map, you can get that automatically via getParameterMap().
arkaes Duraimoni
Greenhorn

Joined: Jan 23, 2013
Posts: 14
@Bear ,
Could you please tell me if we are using request.getParameterValues() that will return the array of values.
but if we wanted to know that which value is for which one. how to do that other than that mapping the index values.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61752
    
  67

Not sure what you mean by that. Since it is an array, it will be in order [0], [1], [2] and so on. The order will be the canonical order of the elements in the HTML DOM.
arkaes Duraimoni
Greenhorn

Joined: Jan 23, 2013
Posts: 14
thank you Bear ,
my question is suppose my form having 10 static fields and some dynamic fields(this may vary depends on user).

Now my form having mixed of this static and dynamic fields,

now how we can segregate the values for the particular fields.

for examples
<input type="text" name="username" /> //static
<input type="text" name="xxxxx" /> //dynamic.. it may be created by add new button
...
..
..
<input type="text" name="age" /> //static
<input type="text" name="yyyy" /> //dynamic.. it may be created by add new button
...
..
..

so above we have n number of xxxx and yyyy.

this time how can we get values for particular fields.


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61752
    
  67

Again, just give them all the same name. The first one will be available in [0]. the second in [1] and so on...
Hikari Shidou
Ranch Hand

Joined: Jan 22, 2013
Posts: 88
Hikari Shidou wrote:In HTML we can put request parameters in array, and easily retrieve them in PHP, as follows

HTML form:


Then we retrieve in PHP:



Sorry for the delay. I did the test, if I ue those lind of names, in Java I'd get 2 independant parameters with names 'mainPatameter[indexName1]' and 'mainPatameter[indexName2]', and not some kind of map named 'mainPatameter' holding all parameters named under that prefix.

It's really a PHP trick that doesn't work in Java. Maybe there's some lib/framework, as BeanUtils.populate(), where I could provide 'mainPatameter' and return me a map/bean with those parameters. I'll take a look on it later.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61752
    
  67

Advice: forget all about the "name[subname]" format. Forget it. Just pretend you never heard of it, and never think about it again. Ever. It's just going to keep you from proceeding.

To get an array -- a real array of values -- just name them all the same. For example "fred". Then the array can get fetched with just "fred". No tricks. No brackets.

To get a bean populated, use BeanUtils.populate(). The param names will automatically be mapped to bean properties of the same name. I think this is closer to what you are after.

Of course, make sure you understand what a bean is and how to define one correctly!
Hikari Shidou
Ranch Hand

Joined: Jan 22, 2013
Posts: 88
lol I know what a bean is :p

The issue is that I'll build a very big form, with tabs and almost a hundred inputs. But I can organize them in sections, so I wanted to merge them all to help gathering parameters.

Some have the same meaning on different sections, so in first thought they'd have the same name. With PHP I can give the section name to a variable and have all its fields gathered from a simple array gather, and then handle data inside it. I even think of, when sending to database, serialize them into JSON, beause I'll never need to query based on these fields, and creating a table with a hundred columns is kinda hard to manipulate in SQL.

Since some fields have the same meaning, their HTML name must be 'section_meaning', but I won't name them like that in the bean. I suppose automatic features like BeanUtils' won't handle this kind of thing, earching for prefixes and then matching sufixes.

I'll just create methods that receive response and return bean and populate them with a bunch of getParameter(). Will be kinda boring to do but I'll get the thing done.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Retrieve array parameters from a request