/    Sign up×
Community /Pin to ProfileBookmark

shopping basket

I’ve created a shopping html form and servlet for it,and I want to do this :Return a web page summarising the cost for each item.
and Add the totals up and provide a grand total.
But all information (apart from quantities) are listed as 0 or null when trying to display the output, could anyone help? I have included my code if anyone fancies giving me a few tips!!

part code…

Html form

<FORM action=’../servlet/SimpleShoppingBasket’ method=’POST’>
<TR><TH ALIGN=’center’ VALIGN=’top’>Item</TH>
<TH ALIGN=’center’ VALIGN=’top’>Unit Price (£)</TH>
<TH ALIGN=’center’ VALIGN=’top’>Quantity</TH></TR>
<TR><TD ALIGN=’center’ VALIGN=’top’ COLSPAN=3>
<tr>
<td> <b>Socks</b> </td>

<td> 4 </td>
<td> <SELECT NAME=’choices’>”
“<OPTION>0</OPTION>”
“<OPTION>1</OPTION>”
“<OPTION>2</OPTION>”
“<OPTION>3</OPTION>”
“<OPTION>4</OPTION>”
“</SELECT>

</td>
</td>
</tr>

<td> <b>Shirt </b> </td>
<td> 16 </td>
<td> <SELECT NAME=’choices1′>”
“<OPTION>0</OPTION>”
“<OPTION>1</OPTION>”
“<OPTION>2</OPTION>”
“<OPTION>3</OPTION>”
“<OPTION>4</OPTION>”
“</SELECT>

</td>

</td>
</tr>
<input type=submit value=’Add to Cart’> </td>
</TABLE>

</FORM>

the servlet

import java.io.*;
import java.util.*
;
import javax.servlet.*;
import javax.servlet.http.*
;

public class SimpleShoppingBasket extends HttpServlet {

public void init() throws ServletException{
System.out.println(“Servlet Initialised”);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{

response.setContentType(“text/html”);
PrintWriter out = response.getWriter();
out.println(“<HTML><BODY>”);

Enumeration aEnum = request.getParameterNames();
int[] unitprices = null;
int[] quantities = null;
String[] products = null;

while (aEnum.hasMoreElements()){
String parameterName = ( String )aEnum.nextElement();
System.out.println(parameterName);
String[] values = request.getParameterValues(parameterName);
if (unitprices == null) unitprices = new int[values.length];
if (quantities == null) quantities = new int[values.length];
if (products == null) products= new String[values.length];

for(int i = 0; i < values.length; i++){
if(parameterName.equals(“price”)){
unitprices[i] = Integer.parseInt(values[i]);
}

else if(parameterName.equals(“choices”)){
quantities[i] = Integer.parseInt(values[i]);
}

else if(parameterName.equals(“items”)){
products[i] = values[i];
}

System.out.println(products[i] + ” ” + quantities[i] + ” ” + unitprices[i]);
}
}

out.println(“<TABLE ALIGN=’center’ BORDER=’1′ WIDTH=’50%’ CELLSPACING=’3′ CELLPADDING=’3′><TR>” + “n” +
“<TR><TH>Item</TH><TH>Unit Price (£)</TH><TH>Quantity</TH><TH>Total</TH>”);

int total = 0;
for(int i =0; i < products.length; i ++){
total += quantities[i] * unitprices[i];
out.println(“<TR><TD>” + products[i] + “</TD><TD>” + unitprices[i] + “</TD><TD>” +
quantities[i] + “</TD><TD ALIGN=’right’>” + unitprices[i] *
quantities[i] + “</TD></TR>”);
}
out.println(“<TR><TD ALIGN=’left’ VALIGN=’top’ COLSPAN=3>Grand Total</TD>”);
out.println(“<TD ALIGN=’right’ VALIGN=’top’>” + total + “</TD></TR>”);
out.println(“</TABLE></BODY></HTML>”);
}
}

to post a comment
Java

0Be the first to comment 😎

×

Success!

Help @gpo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...