handling multiple html parameters in servlets
Hi friends
I am new and beginer in java programing . i am struggling to overcome a problem in accessing html parameters
Problem is that . I designed a form with 5 rows each row contain two fields Name and quantity respectly. but i want to add five rows i.e items details to list. adding one by one getting the name and search for price from database is of noproblem but
i want to send all the 10 field data that includes 5 item names and quanties of those items on single button by GET method. in my srvlet i want to short it out that
if first name in theparameter is exist in my data i want to read exact quantiy and assign price from my data
here my problem is the servlet is considering 1 item and its qty only.
rest it is not considering
please tell me how to validate such type of html requests? is there any method or class to solve the problem ?
or shall i required to go for javascript validation?
it so tell me the logic to solve it ?
thanks in advance please .......
If you use the same name for all input fields, you could use getParameterValues method (instead of getParameter).Else you have to give the input fields unique names, e.g. name1, quantity1, name2, quantitiy2 and so on.
hi friends i got a code to display all the request parameters the code is
given below
Enumeration paramNames = request.getParameterNames();
while(paramNames.hasMoreElements()) {
String paramName = (String)paramNames.nextElement();
out.println("<TR><TD>" + paramName + "\n<TD>");
String[] paramValues = request.getParameterValues(paramName);
if (paramValues.length == 1) {
String paramValue = paramValues[0];
if (paramValue.length() == 0)
out.print("<I>No Value</I>");
else
out.print(paramValue);
} else {
out.println("<UL>");
for(int i=0; i<paramValues.length; i++) {
out.println("><LI>" + paramValues);
-
in this it search for name and corresponding all the values but what i required is
for single name i given 5 items. and single quantity(param name ) 5 quantity inputs. i need to check first item qty and taking that item i need to check the price from property file and i want to add corresponding qty only and the loop should do thww work for each and every item for thos how can i proceed is there any idea to implement it in a efficient way..
waiting for your kind knowledge share.........thank you
If you name your input fields name1, qty1, name2, qty2, and so on. Then, you could do something like this:
for (int i = 1; i <=5; i++) {
String name = request.getParameter("name" + i);
int qty = Integer.parseInt(request.getParameter("qty" + i));
// do whatever you want with qty and name
}
This is just a rough guess. You should check the values given as quantitiy (if valid numer etc.).
thanks for your reply. . . .thank you very much. but in my application it is not possible to give names as name1 name2 ...y coz i always add some items in my html page for that each and every time i eant to give the name . instead with 1 name only i want to validate it. .. . is there any possible way to
> use loop statements in which we get the param name first andthen corresponding value
--> immediatetly come pare it to item list in property files
>remember the index of the item in our loop
>if found item then take the parameter values and print only the parameter value with the index of name same as value and add those items in the List
in the above i mentioned problem in above code i am getting output as
-
namevalue(out put for name with one value)
name(item)value1
value2
value3
name(quantity) qty 1
qty 2
qty 3
--
can you please suggest me which way i can approach . also think abt my idea ? if it works ,what the improvemente i suppose to do in my code?