Solved

Java Servlet - getting information from multiple textboxes

Posted on 2009-05-05
6
680 Views
Last Modified: 2012-05-06
Hi,

I am wondering if there is a way to pull data from multiple text fields on a html page without having to hardcode the textfield names into my code using getParamater(textboxnamehere).

For example my page has 4 text fields, I want to be able to get the informtaion in those text fields and run it through the same SQL statement individually, using a loop.

Basically while there is a textbox that has not been read get textbox information, run SQL statement then process the next textbox

instead of using code like
String textOne = request.getParameter("textfield");
String textTwo = request.getParameter("textfield2");
0
Comment
Question by:Robert
[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 Comments
 
LVL 92

Accepted Solution

by:
objects earned 450 total points
ID: 24311411
use the same name for each field and use

String[] values = request.getParameterValues("textfiald");
for (String value : values) {
    // make sql call

0
 

Expert Comment

by:Debasis126
ID: 24311707
Another useful way, while avoiding hardcoding is to use request.getParameterNames();

It will return an enumeration of all attribute names. Iterate through that and get all respective values for each parameter.

Hope this  helps.
0
 
LVL 92

Expert Comment

by:objects
ID: 24311713
the downside with using getParameterNames() is if you have other unrelated parameters in the query string

0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

Assisted Solution

by:Debasis126
Debasis126 earned 50 total points
ID: 24311730
String parameterName = "";
for(Enumeration e = request.getParameterNames();e.hasMoreElements(); )
{
                        parameterName = (String)e.nextElement();
                        String parameterValue = (String)request.getParameter(parameterName);
                               \\ insert your sql statements

}

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24311731
>>For example my page has 4 text fields, I want to be able to get the informtaion in those text fields and run it through the same SQL statement individually, using a loop.

You would get a more efficient solution if you put all four fields into a PreparedStatement and made one query
0
 
LVL 1

Author Closing Comment

by:Robert
ID: 31578335
Thanks all
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

705 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