Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > HTML form linking to JSP...

Reply
Thread Tools

HTML form linking to JSP...

 
 
Jagdeesh
Guest
Posts: n/a
 
      08-30-2003
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
 
Reply With Quote
 
 
 
 
Steve Claflin
Guest
Posts: n/a
 
      08-31-2003
Jagdeesh wrote:
>
> 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>
<td class="stotal" align="center"><input type="text" name="qty105"
value="0" size="4"></td>
> </tr>


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.

--
Steve
--
http://www.steveclaflin.com
 
Reply With Quote
 
 
 
 
Wendy S
Guest
Posts: n/a
 
      08-31-2003
Steve Claflin wrote:

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

--
Wendy in Chandler, AZ
 
Reply With Quote
 
Steve Claflin
Guest
Posts: n/a
 
      08-31-2003
Wendy S wrote:
>
> Steve Claflin wrote:
>
> > 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.



> --
> Wendy in Chandler, AZ


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).

--
Steve
--
http://www.steveclaflin.com
 
Reply With Quote
 
Doug Turner
Guest
Posts: n/a
 
      08-31-2003
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."

On 30 Aug 2003 15:34:13 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (Jagdeesh) wrote:

>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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to retrieve form field value if form is EncType=multipart/form-dataForm? Li Zhang ASP .Net 4 02-27-2009 01:23 AM
Linking to a CGI from an index.html araminska HTML 10 12-14-2007 01:41 PM
Newby Question Linking of data between HTML and ASP! John Baker ASP .Net 3 02-08-2005 01:13 PM
Linking a form to email Angus Og Computer Support 2 01-22-2005 04:37 AM
Linking of several tables in a heirarchical form Jacob ASP General 1 06-30-2003 11:14 PM



Advertisments