• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can Someone help me out?

Justen All
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this Servlet I am using,
import javax.servlet.*;
import javax.servlet.http.*;
import java.text.*;
import java.io.*;
import java.util.*;
public class HttpPostServlet extends HttpServlet {
private String names[] = {"dog", "cat", "bird", "snake", "none"};
public void doPost (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int animals[] = null, total = 0;
File f = new File ("poll.txt");
if (f.exists()){
try {
ObjectInputStream input = new ObjectInputStream(new FileInputStream(f));
animals = (int[]) input.readObject();
for (int i = 0; i < animals.length; ++i)
total += animals[i];
catch (ClassNotFoundException e){
animals = new int[5];
String value = request.getParameter ("animal");
for (int i = 0; i < names.length; ++i)
if (value.equals(names[i]))++animals[i];
ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(f));
output.writeObject (animals);
double percentages[] = new double[animals.length];
for (int i = 0; i < percentages.length; ++i)
percentages[i] = 100.0 * animals[i]/total;
PrintWriter responseOutput = response.getWriter();
StringBuffer buf = new StringBuffer();
buf.append("<title>Thank you!\n");
buf.append("Thank you for participating.\n");
DecimalFormat twoDigits = new DecimalFormat ("#0.00");
for (int i = 0; i < percentages.length; ++i){
buf.append ("<BR>");
buf.append (names[i]);
buf.append (": ");
buf.append (twoDigits.format (percentages[i]));
buf.append (animals[i]);
buf.append ("\n");
buf.append ("\n<BR><BR>Total responses: ");
buf.append (total);
buf.append ("</PRE>\n</html>");
responseOutput.println (buf.toString());
However when I combine it with this HTML it doesnt work in the browser
<INPUT TYPE=radio NAME=animal VALUE=dog>Dog<BR>
<INPUT TYPE=radio NAME=animal VALUE=cat>Cat<BR>
<INPUT TYPE=radio NAME=animal VALUE=bird>Bird<BR>
<INPUT TYPE=radio NAME=animal VALUE=snake>Snake<BR>
<INPUT TYPE=radio NAME=animal VALUE=none>None<BR>
<BR><BR><INPUT TYPE=submit VALUE="Submit">
<INPUT TYPE=reset>
Can anyone help me out?
Matthew Phillips
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to Javaranch. We do have one rule, which is strictly enforced. Please read the Javaranch naming policy and change your display name to comply. Thanks.
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first thing you need to do is to view the source of the page that is sent to the browser - even if there is nothing displayed there might be content you can't see. Then if that doesn't help look at the server logs for any errors recorded there that might not be shown on the browser.
In this case your problem is your html tags that you send to the browser. You dont close the title tag, and you dont have a head or body tag. Some browsers are very forgiving and wil try to interpret your content as best they can. Your still better off being as true to the standard as you can be to avoid any cross-browser issues (it'll also help you out id you ever get into xml too).
Here is the code that'll make it work for you:

hope that helps
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic