File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes naming dynamic  form elements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "naming dynamic  form elements" Watch "naming dynamic  form elements" New topic

naming dynamic form elements

Rajeev Ravindran
Ranch Hand

Joined: Aug 27, 2002
Posts: 455
hi all,
i have a jsp file in which the elements (eg. textfileds) are generated dynamically..wat is the best practice to name these elelemts..
is it a good practice append an int variable to the end of the textfilds name like txtfld1, txtfld2 ??
if i name all the textfields with the same name, is there any way to get the values of each of these textfields ??

SCJP, SCWCD, SCBCD, Oracle Certified Professional (SQL n PL/SQL)
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63844

Yes, you can retrieve all the values if you name the fields the same by using request.getParameterValues() (rather than request.getParameter()). However, you will not be able to tell which value came from which field.
If that is an important distinction, then you will need to assign each field a unique name.
Which makes the most sense depends upon how your submission target needs to deal with the fields, which you did not discuss in your original question.

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: naming dynamic form elements
It's not a secret anymore!