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 ]
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
posted
0
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?
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
posted
0
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 ]
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
posted
0
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
posted
0
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?
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
posted
0
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.
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.
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.