2

I'm currently working on a Java web application which will take data submitted by a user (from a form) and write it to a database. The input page is a JSP file with lots of textboxes, dropdowns, textareas, etc. I would like to know how to go about processing data from the form and putting it into a database when the form could have a variable number of inputs.

I'll give you an example that illustrates my problem: A user has multiple phone numbers that they want to give us. On the form, there is a single input box for phone number, and an "add" button which uses JQuery to add another phone number input box. When the user submits the form, a servlet receives all the data. At this point, what is the most robust way to process this data so that I can add it to the database? My first thought would be to have the javascript increment the name of the phone number box (ex: "phonenumber1", "phonenumber2", etc) and have the servlet loop through the inputs, but it seems like their should be a more robust way to do it. I would really appreciate any advice I can get. Thanks!

Edit: spelling

2 Answers 2

1
request.getParameterNames();--phonenumber1,2,3 

it will return all parameters Names

and iterate one by one that enum and get like this

request.getParameter(phonenumber1);--insert into DB 
Sign up to request clarification or add additional context in comments.

Comments

0

I'd use the following:

String[] phonenumbers = request.getParameterValues("phonenumber") 

see javadoc. It will return to you an array of all the fields named "phonenumber" in your form. You can eventually cycle the array and store it to your database.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.