wood burning stoves*
The moose likes Servlets and the fly likes How to get pair values using getParameterNames? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to get pair values using getParameterNames?" Watch "How to get pair values using getParameterNames?" New topic
Author

How to get pair values using getParameterNames?

Anthony Karta
Ranch Hand

Joined: Aug 09, 2004
Posts: 342
Hi all,

I have pair values (task id and description) such as in JSP code below.

How I obtain their values in servlet??

I tried getParameterNames() but then I realize that the paramNames in Enumeration are Not in order.

any advice appreciated. TIA, anthony.





SCJP 5
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60760
    
  65

I tried getParameterNames() but then I realize that the paramNames in Enumeration are Not in order.


HTTP guarantees no order. If your app depends upon parameters being sent in a specific order, it's time to rethink that design.
[ June 04, 2007: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Hi anthony,

The code you pasted seems to be fine. what exactly is the problem you are getting?


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Anthony Karta
Ranch Hand

Joined: Aug 09, 2004
Posts: 342
Originally posted by Raghavan Muthu:
Hi anthony,

The code you pasted seems to be fine. what exactly is the problem you are getting?


Hi Bear and Raghavan,

I would like to capture list of taskID and its description.

in HTML form, I name them such as taskID_1, Description_1, task_ID2, description_2 and so on.

I spent almost a day until I realized "taskBean" is Not populated properly.

Hope make sense. what technique do guys usually use?

TIA
Mathias Nilsson
Ranch Hand

Joined: Aug 21, 2004
Posts: 367


This will give you 4 different beans in the taskList. Two that can return a value from getTaskId() and two that can return a value from getDescription()


SCJP1.4
Anthony Karta
Ranch Hand

Joined: Aug 09, 2004
Posts: 342
Originally posted by Mathias Nilsson:

This will give you 4 different beans in the taskList. Two that can return a value from getTaskId() and two that can return a value from getDescription()


I realized that. I just tried to find an efficient/easy way to capture list of user input.

I can check param name one by one but seems tedious. what is the alternative/better way?

thanks again
Srilakshmi Vara
Ranch Hand

Joined: Jul 21, 2004
Posts: 169
Code seems fine, but the order won't come as same,

What is your problem exactly? Are you getting null values in the loop or if you are getting any exception it helps to reply.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Are you trying to write a servlet that expects to be passed those four parameters? If so, just write code using the request.getParameter() method four times. Or are you trying to write some kind of a framework where the servlet can be passed any number of parameters with any names, and you will encapsulate those somehow and pass them to the real servlet?
Mathias Nilsson
Ranch Hand

Joined: Aug 21, 2004
Posts: 367
Why not use the same name for description and other parameters.
You could save number of values in an input type hidden, not the best solution but anyway.


If taskID and description are the same, equivalent parameters then this could work. Wouldn't even have to pass a hidden value. I haven't tested the code but it should work I hope
[ June 05, 2007: Message edited by: Mathias Nilsson ]
sannareddy sudheer
Greenhorn

Joined: Jul 16, 2003
Posts: 8
Hello try this one , no need to get he list(enumeration) of parameter names if the pair of the names like 'taskID_1' , 'description_1' and 'taskID_2' , 'description_2' etc like that use the follwing logic , it will work fine.

for(int index = 1 ; index <= noOfPairs ; index++){
taskBean = new TaskBean();
String taskID = request.getParameter("taskID_"+index);
if(taskID != null){
taskBean.setTaskID(Integer.parseInt(taskID));
taskBean.setDescription(request.getParameter("description_"+index);
}
taskList.add(taskBean);
}

try to get the 'noOfPairs' value using hidden variable if the number of pairs are static.

hope it helps.
Mathias Nilsson
Ranch Hand

Joined: Aug 21, 2004
Posts: 367
Humm... just read a chapter in J2EE Core patterns and passing values in input type hidden maybe isn't the best solution. I guess the getParameterValues is there for some reason. Radio buttons, multi select and for the problem you are facing. If it is always pairs, same amount of taksId as description then don't pass a hidden value.
Anthony Karta
Ranch Hand

Joined: Aug 09, 2004
Posts: 342
Originally posted by Bear Bibeault:


HTTP guarantees no order. If your app depends upon parameters being sent in a specific order, it's time to rethink that design.

[ June 04, 2007: Message edited by: Bear Bibeault ]


Thanks all for your reply. Quite busy with other stuff lately, I'm just back working on this project.

Mathias, your code won't work, because when HTML Post send to the server, the order of task and description will Not match.

Sannareddy's code is working . thanks.

But I'm interested with Bear's comment.

How should I re-design it? do you mind share your idea?

thanks again
Rajesh Agarwal
Ranch Hand

Joined: Aug 01, 2005
Posts: 79
If the user input parameters are over the query string then you can use the method Hashtable parseQueryString(String s)
wang godman
Greenhorn

Joined: Jun 21, 2007
Posts: 3
why not use some framework such as struts for formbean.It can help you to form-assembling.GoodLuck
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60760
    
  65

Struts will be of no help since the issue lies with the workings of the underlying HTTP protocol which does not guarantee order.

To answer the design question, why is the order important in the first place?
Anthony Karta
Ranch Hand

Joined: Aug 09, 2004
Posts: 342
Originally posted by Bear Bibeault:
Struts will be of no help since the issue lies with the workings of the underlying HTTP protocol which does not guarantee order.

To answer the design question, why is the order important in the first place?


Hi Bear,

The order is important because the form is used to display, add and update list of tasks back to database.

The form has 2 columns TaskID and TaskDescription. TaskID contains primary key used to search Task table and update the task description. In case of add , the process will increment the taskID and insert the new task.

I am not sure if it's the correct way. any advice welcome.

thanks again
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60760
    
  65

Why are you exposing the primary key in the form? Is it editable? Is it necesary information for the user to know?

If all you need is to have the task description associated with its primary key, you can use the primary key as part of the name of each description field.

P.S. I'd also consier using an encryption scheme for your primary keys -- it's generally considered less than secure to expose them to the client.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41063
    
  43
No, that's not the correct way to do this. As Bear has said repeatedly: HTTP does not guarantee the order of elements having the same name, and there's nothing you can do about it.

The correct way to do this is to give the elements different names, e.g. taskID_1 and taskID_2. It's easy enough to figure out which is which on the server.

This is similar to SELECT statements in SQL: The order in which they return their results is not defined (and the SQL standard says so). Even though the DB may return results in the same order 1000 times, that indicates nothing about what it may do on the 1001st time.


Ping & DNS - my free Android networking tools app
 
Consider Paul's rocket mass heater.
 
subject: How to get pair values using getParameterNames?
 
Similar Threads
Jsp refering to itself
Is it possible to obtain URL Encoded Params in an Applet?
Need to split up servlet
passing hidden forms problem
Inserting a checkbox value into database.