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 req.getParameterValues? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "req.getParameterValues?" Watch "req.getParameterValues?" New topic
Author

req.getParameterValues?

Viet Jav
Ranch Hand

Joined: Dec 27, 2001
Posts: 55
JSP:
<% for (int i = 0; i < 5; i ++) {%>
<input type ="text" name="txtName" value = "<%=i%>"
<select name ="sAmount">
<option value="<%=i%>">....</option>
</select>
<%}%>
<input type="submit" name ="Submit" value="submit"/>

When click Submit buton, doPost called.
Servlet:
doPost() {
String Amount[] = req.getParameterValues("sAmount");
String Name[] = req.getParameterValues("txtname");
...
}

The length of Amount and Name are 5.
The value of Amount and Name usually are
Name [] = {0,1,2,3,4}
Amount[] = {0,1,2,3,4}
or value got random in range {0,1,2,3,4,}?

Please help.
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Viet Jav,

What is your question?
Viet Jav
Ranch Hand

Joined: Dec 27, 2001
Posts: 55
Julian Kennedy,

I am sorry.
My question is what are values of Amount and Name in order?

JV.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12803
    
    5
I don't think there is any guarantee that the order will be the same as on the form when you have multiple values with the same name.
Bill
Viet Jav
Ranch Hand

Joined: Dec 27, 2001
Posts: 55
William Brogden,

Suppose that order will be NOT the same as on the form when you have multiple values with the same name.
But order of Name's values and order of Amount's values are correlative?
ex:
If Order in Name is Name [] = {3,0,2,4,1}, order in Amount will also be Amount[] = {3,0,2,4,1}.

JV.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61417
    
  67

No such guarentees are made. If you need them to be in a specific order, or to correlate to each other, you'll need to come up with another scheme for naming the form elements.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Vijaya Mohan
Greenhorn

Joined: Sep 01, 2004
Posts: 2
Hello Viet

Common solution for the above problem is while creating the jsp page create the textbox by using array
i.e name=txtName[i]

in the servlet you can retrieve by using the same order.it will be in the same order as your passing from the html page.

bye
vijay
Viet Jav
Ranch Hand

Joined: Dec 27, 2001
Posts: 55
Hi Vijaya,

Thank for your help.

Can you explain in detail?

In Servlet, What do I do to get in the same order?

JV.
[ September 01, 2004: Message edited by: Viet Jav ]
Saritha Penumudi
Ranch Hand

Joined: Aug 18, 2003
Posts: 147
You should loop over the textName[i] to get the values.

For example, In your jsp you would be doing something like this.

<% for (int i = 0; i < 5; i++){ %>
<input type="text" name='<%="textName"+i%>' value=<%=i%>>
<%}%>

In your servlet you should get the values by doing something like this.

for (int i = 0; i < 5;i++){
val = request.getParameter(("textName"+i));
}

I guess I have interpreted what vijaya has suggest correctly.
I guess this should work
Viet Jav
Ranch Hand

Joined: Dec 27, 2001
Posts: 55
Thank you.
I understanded.

JV.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: req.getParameterValues?