Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to collect data from JSP ?

Posted on 2011-09-13
13
Medium Priority
?
262 Views
Last Modified: 2012-08-14
I have database table column as

Itemid | Amount

I shall have a screen attached .

When my user submit the form. I want to insert the data into the above  table.

How do I collect the data in the servlet  so that it can preserve the mapping  i.e   itemid with Amount ?

save.png
0
Comment
Question by:cofactor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
13 Comments
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 36528759
You can preserve the indexes in the names
item1, amount1, etc

at the server side, you need to know how many pairs are there, then you can simply fetch
request.getParameter( "item" + index ); //in the loop
request.getParameter( "amount" + index ); //in the loop

something like this?
http://www.techlabs4u.com/2011/09/passing-arraylist-of-objects-from-jsp.html
0
 

Author Comment

by:cofactor
ID: 36533748
Thanks for the comments. I get some pointer . But my actual screen has one more field.  The above screen is just small part of it.

Here is the proposed screen and proposed database table ATTACHED.


Please see  screen and database table. As you see for a given ID in the screen I have multiple  (select box , amount ) pair.

How do I capture data  at server side for such a screen ? Can you please tell me a workaround ?

N.B: I generate those dynamic rows <tr>  and  select boxes using javascript.
save1.png
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 36533954
That should not be an issue.

You just need to name those input fields (select and amount) in such a way that by just appending the index (or a counter in a for loop) you should be able to retrieve values in servlet
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:cofactor
ID: 36534096
Still confusing. Need more help here. I'm stuck at this part

>>>You just need to name those input fields

What name you suggest for  ID field ?

What name you suggest for select box and amount pairs  for the above ID field ?
0
 

Author Comment

by:cofactor
ID: 36534315
comments ?
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 36535007
This means, that for each Id there has to be select box and textbox right.
In this case, i would name my select box and textbox as item_{id value} and amount_{id value}

so, if you are iterating through the ids, you can easily fetch the select box and textbox value by appending the id value to the item_ and amount_
0
 

Author Comment

by:cofactor
ID: 36535033
No. This will not work.

>>>i would name my select box and textbox as item_{id value} and amount_{id value}

you will have a  multiple select box and textbox for a  given id_value.

How do you address that  ?
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 36540898
Good question. Got me thinking.
I guess one way to deal with this is to have a hidden input for each id, which will tell us how many item and amount pair are for each of them.
0
 

Author Comment

by:cofactor
ID: 36547152
Not understood.  I dont get your idea.  Could you please tell me

in JSP:
-------------
what you write for ID field name ?

what you write for select box  name ?

what you write for amount box  name ?

In Servlet
--------------------
How you retrieve the multiple values  for ID's and corresponding select box and amount pair now ?





0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 36547547
<<what you write for ID field name ?>>
1

<<what you write for select box  name ?>>
item1

<<what you write for amount box  name ?>>
amount1

hidden field value == 1

-----------------------------------------------

now if the hidden field value is 2, which means that for one id there are more than one pair

For item id 1 and hidden field value 2
item1_1
amount1_1
item1_2
amount1_2




0
 

Author Comment

by:cofactor
ID: 36547943
>>><<what you write for ID field name ?>>
>>1

You should not write a field name as "1" !  ...although nothing stops .

Please note i have to get the value of ID field also.
I should  NOT be doing this    

String field_value=request.getParameter("1"); // this  ..very bad
0
 
LVL 40

Accepted Solution

by:
Gurvinder Pal Singh earned 2000 total points
ID: 36548057
i just gave an example, you can give any name you want, and you just have to append this name to the item_ and amount_
0
 
LVL 5

Expert Comment

by:kcm76
ID: 36556627
you need to have another column added to the table - SeqNo - which would be having value of 1,2,3 etc for a given ID. Please note the combination of ID and SeqNo can be maintained as Unique value if required...
This you can use when naming the type and quantity
for ex:
ID="<ID>"
name="Name_<ID>"
address="Address_<ID>"
type="type_<ID>_<SeqID>" for ex:TYPE_101_1, TYPE_101_2 etc...
Quantity="Quantity_<ID>_<SeqID>" for ex:Quantity_101_1, Quantity_101_2 etc...

Please let me know if you face any issue in this...
0

Featured Post

Cloud Training Guides

FREE GUIDES: In-depth and hand-crafted Linux, AWS, OpenStack, DevOps, Azure, and Cloud training guides created by Linux Academy instructors and the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn different types of Android Layout and some basics of an Android App.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question