Java Servlet - getting information from multiple textboxes

Posted on 2009-05-05
Last Modified: 2012-05-06

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");
Question by:Robert
LVL 92

Accepted Solution

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


Expert Comment

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.
LVL 92

Expert Comment

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why


Assisted Solution

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


LVL 86

Expert Comment

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

Author Closing Comment

ID: 31578335
Thanks all

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Apps blocked by Java 9 79
Why my table column Id is not passed to java object? 4 39
Tomcat: Unable to run tomcat service. 2 19
Java: The Public Class Main 4 16
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

773 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