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
Solved

Java Servlet - getting information from multiple textboxes

Posted on 2009-05-05
6
654 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
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

860 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