HTML form linking to JSP...

J

Jagdeesh

Hai,

I have the following table as HTML file...........

<table align="center" width="600" cellpadding="10" cellspacing="10">
<tr>
<td>
<form action="/mbis/actions/order_confirm.jsp"
method="#default"><table align="center" width="600" cellpadding="1"
cellspacing="1">

<tr>
<td align="center" class="head"><strong>Description</strong></td>
<td align="center" class="head"><strong>Item Number</strong></td>
<td align="center" class="head"><strong>Price</strong></td>
<td align="center" class="head"><strong>Wanted Price</strong></td>
<td align="center" class="head"><strong>Quantity</strong></td>
</tr>
<tr>
<td class="stotal">Scanner</td>
<td class="stotal" align="center">105</td>
<td class="stotal" align="right">€ 200.00</td>
<td class="stotal" align="center"><input type="text" name="want"
value="0" size="4"></td>
<td class="stotal" align="center"><input type="text"
name="105|Scanner|200.00" value="0" size="4"></td>
</tr>
<tr>
<td colspan="4" align="center"><input type="submit" value="Place
Order"></td></tr></table></form></td></tr>
</table>



This HTML is then linked to order_confirm.jsp which retrieves the
values "want" and "Quantity" which has the following structure:





<table align="center" width="600" cellpadding="10" cellspacing="10">
<tr>
<td>
<form action="/mbis/actions/order_send.jsp"><table
align="center" width="600" cellspacing="1" cellpadding="1">
<tr>
<td align="center" class="head"><strong>Item Number</strong></td>
<td align="center" class="head"><strong>Description</strong></td>
<td align="center" class="head"><strong>Price</strong></td>
<td align="center" class="head"><strong>Wanted Price</strong></td>
<td align="center" class="head"><strong>Quantity</strong></td>
<td align="right" class="head"><strong>ActualRatetotal</strong></td>
</tr>

<%
float stotal = 0;
float gtotal = 0;
DecimalFormat df = new DecimalFormat("#.##");

for (Enumeration e = request.getParameterNames();e.hasMoreElements();)
{
Object name = e.nextElement();
int wantpr = Integer.parseInt(request.getParameter("want"));
int qty = Integer.parseInt(request.getParameter((String)name));
if(!(qty > 0)) { continue; }
StringTokenizer st = new StringTokenizer((String)name,"|");
String part_no = st.nextToken();
String desc = st.nextToken();
float price = Float.parseFloat(st.nextToken());

%>
<input type="hidden" name="<%= part_no + "|" + desc + "|" + price
%>" value="<%= qty %>">
<tr>
<td class="stotal"><%= part_no %></td>
<td class="stotal"><%= desc %></td>
<td class="stotal" align="right">€ <%= df.format(price) %></td>
<td class="stotal" align="center"><%= wantpr %></td>
<td class="stotal" align="center"><%= qty %></td>
<% stotal = qty*price;gtotal += stotal; %>

<td class="stotal" align="right">€ <%= df.format(stotal) %></td>
</tr>

<%
}
%>

<tr><td class="gtotal" colspan="5"
align="right"><strong>TOTAL</strong></td><td class="gtotal"
align="right">€ <%= df.format(gtotal)
%></td></tr></table></td></tr></table>
<table align="center">
<tr>
<td>
<%
if(gtotal > 0) {
%>
<input type="submit" value="Confirm"></form></td><td>
<form action="/mbis/catalog.htm">
<input type="submit" value="Cancel"></form>
<% } else { %>
</form><div align="center"><strong>You haven't ordered
anything</strong></div>
<% } %>
</td></tr></table>



But, the problem is when i give the values for the parameters
"Quantity" and "Wanted Price" , only the parameter "Quantity" is
mapped to this JSP file but not the "Wanted Price" parameter. Can any
one find the problem and give me a solution?









Waiting for your valuable reply.

Regards,
Jagdeesh
 
S

Steve Claflin

Jagdeesh said:
Hai,

I have the following table as HTML file...........

<table align="center" width="600" cellpadding="10" cellspacing="10">
<tr>
<td>
<form action="/mbis/actions/order_confirm.jsp"
method="#default"><table align="center" width="600" cellpadding="1"
cellspacing="1">

<tr>
<td align="center" class="head"><strong>Description</strong></td>
<td align="center" class="head"><strong>Item Number</strong></td>
<td align="center" class="head"><strong>Price</strong></td>
<td align="center" class="head"><strong>Wanted Price</strong></td>
<td align="center" class="head"><strong>Quantity</strong></td>
</tr>
<tr>
<td class="stotal">Scanner</td>
<td class="stotal" align="center">105</td>
<td class="stotal" align="right">€ 200.00</td>
<td class="stotal" align="center"><input type="text" name="want"
value="0" size="4"></td>
<td class="stotal" align="center"><input type="text"
name="105|Scanner|200.00" value="0" size="4"></td>
</tr>
<tr>
<td colspan="4" align="center"><input type="submit" value="Place
Order"></td></tr></table></form></td></tr>
</table>

This HTML is then linked to order_confirm.jsp which retrieves the
values "want" and "Quantity" which has the following structure:

<table align="center" width="600" cellpadding="10" cellspacing="10">
<tr>
<td>
<form action="/mbis/actions/order_send.jsp"><table
align="center" width="600" cellspacing="1" cellpadding="1">
<tr>
<td align="center" class="head"><strong>Item Number</strong></td>
<td align="center" class="head"><strong>Description</strong></td>
<td align="center" class="head"><strong>Price</strong></td>
<td align="center" class="head"><strong>Wanted Price</strong></td>
<td align="center" class="head"><strong>Quantity</strong></td>
<td align="right" class="head"><strong>ActualRatetotal</strong></td>
</tr>

<%
float stotal = 0;
float gtotal = 0;
DecimalFormat df = new DecimalFormat("#.##");

for (Enumeration e = request.getParameterNames();e.hasMoreElements();)
{
Object name = e.nextElement();
int wantpr = Integer.parseInt(request.getParameter("want"));
int qty = Integer.parseInt(request.getParameter((String)name));
if(!(qty > 0)) { continue; }
StringTokenizer st = new StringTokenizer((String)name,"|");
String part_no = st.nextToken();
String desc = st.nextToken();
float price = Float.parseFloat(st.nextToken());

%>
<input type="hidden" name="<%= part_no + "|" + desc + "|" + price
%>" value="<%= qty %>">
<tr>
<td class="stotal"><%= part_no %></td>
<td class="stotal"><%= desc %></td>
<td class="stotal" align="right">€ <%= df.format(price) %></td>
<td class="stotal" align="center"><%= wantpr %></td>
<td class="stotal" align="center"><%= qty %></td>
<% stotal = qty*price;gtotal += stotal; %>

<td class="stotal" align="right">€ <%= df.format(stotal) %></td>
</tr>

<%
}
%>

<tr><td class="gtotal" colspan="5"
align="right"><strong>TOTAL</strong></td><td class="gtotal"
align="right">€ <%= df.format(gtotal)
%></td></tr></table></td></tr></table>
<table align="center">
<tr>
<td>
<%
if(gtotal > 0) {
%>
<input type="submit" value="Confirm"></form></td><td>
<form action="/mbis/catalog.htm">
<input type="submit" value="Cancel"></form>
<% } else { %>
</form><div align="center"><strong>You haven't ordered
anything</strong></div>
<% } %>
</td></tr></table>

But, the problem is when i give the values for the parameters
"Quantity" and "Wanted Price" , only the parameter "Quantity" is
mapped to this JSP file but not the "Wanted Price" parameter. Can any
one find the problem and give me a solution?

Waiting for your valuable reply.

Regards,
Jagdeesh

I may be missing something here, but it seems like you're sort of double
counting your parameters. Why use the loop through the parameter names
if there is only one row in the original form table (and therefore only
one "want" and one "Quantity").

If there is more than one row, then I would expect to see the parameter
names returned by the getParameterNames() method to be "want",
"105|Scanner|200.00", "want", "106|Yoda|100.00", "want",
"107|Fishing|150.00", etc. In that case you would need to bump your
e.nextElement() twice for each pass of the loop.

I would think a better strategy would be to rename the want text input
in your html page to be unique for each row (like "want105", "want106",
etc.) -- shouldn't be a problem since you're already doing that for the
other text field. And also, maybe have each row have a hidden field
with the item number, so that your jsp could use that to create the want
parameter name. In fact, if you are going to do that, you could embed
all the information now contained in the name of the quantity field in
the hidden field, so that the quantity field could have a more
understandable name like "qty105", "qty106", etc.

As in:

<tr>
<td class="stotal">Scanner</td>
<td class="stotal" align="center">105
<input type="hidden" name="item" value="105|Scanner|200.00"></td>
<td class="stotal" align="right">€ 200.00</td>
<td class="stotal" align="center"><input type="text" name="want105"
value="0" size="4"></td>

I would probably go as far as to not embed the other data into the item
field and have it passed as separate parameters as well -- produces a
longer query string, but saves the tokenizing on the server end.
 
W

Wendy S

Steve said:
If there is more than one row, then I would expect to see the parameter
names returned by the getParameterNames() method to be "want",
"105|Scanner|200.00", "want", "106|Yoda|100.00", "want",
"107|Fishing|150.00", etc. In that case you would need to bump your
e.nextElement() twice for each pass of the loop.

There is no guarantee that the form fields will arrive in the same order
they appeared on the form. Most browsers do send them in order, but it's
not something you should depend on for your app to function correctly.

I don't see why the OP is iterating through the request parameters, either.

It might be instructive to include some debug code in each JSP for a while--
print out all of the request parameters so you can see what's going on.

Consider moving the processing into a Servlet, or at least a separate JSP.
Do the processing, then forward to a JSP that does nothing but display the
info. What you posted is incredibly hard to read, but then I've been using
Struts for a long time now and scriptlets give me hives. ;)
 
S

Steve Claflin

Wendy said:
There is no guarantee that the form fields will arrive in the same order
they appeared on the form. Most browsers do send them in order, but it's
not something you should depend on for your app to function correctly.

Wendy,

Thanks for the confirmation of my suspicion. I almost mentioned that,
but wasn't sure. I personally always follow the "if not sure, don't do
it" philosophy, which is why I later suggested renaming the fields.

It seems that the use of multiple fields with the same name should be
restricted to situations where you want to accumulate data -- like from
a number of checkboxes, with the data going into a collection of sorts,
possibly being added to a database with one record per item linked to
some key field (like a form asking what programming languages you know).
 
D

Doug Turner

I can't specifically figure out why you aren't getting the "want"
value but are getting "qty", but I am sure it is related to the use of
a parameter name to carry variable information in your HTML file.
This is a Bad Idea.

Instead of imbedding item number, description and price into the name
of the quantity field, create three hidden fields, with meaningful
names, that carry that information. Then the quantity field can also
have a meaningful, defined name. Your jsp is then simplified (no need
for a loop), and it is just looking for the 5 fields with predefined
names that it needs. If you still have a problem, this will be a lot
easier to debug.

You could also look into using the disabled attribute on an input
field instead of hidden fields for the item number, description and
price, but I don't know if all browsers support that, and I don't know
if it is "standard HTML."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top