*
The moose likes Servlets and the fly likes Can Someone help me out? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Can Someone help me out?" Watch "Can Someone help me out?" New topic
Author

Can Someone help me out?

Justen All
Greenhorn

Joined: Apr 04, 2002
Posts: 1
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();
input.close();
for (int i = 0; i < animals.length; ++i)
total += animals[i];
}
catch (ClassNotFoundException e){
e.printStackTrace();}
}
else
animals = new int[5];
String value = request.getParameter ("animal");
++total;
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);
output.flush();
output.close();
double percentages[] = new double[animals.length];
for (int i = 0; i < percentages.length; ++i)
percentages[i] = 100.0 * animals[i]/total;
response.setContentType("text/html");
PrintWriter responseOutput = response.getWriter();
StringBuffer buf = new StringBuffer();
buf.append("<html>\n");
buf.append("<title>Thank you!\n");
buf.append("Thank you for participating.\n");
buf.append("<BR>results:\n<PRE>");
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());
responseOutput.close();
}
}
However when I combine it with this HTML it doesnt work in the browser
<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>
<FORM METHOD="POST"
ACTION="HttpPostServlet">
Test?<BR><BR>
<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>
</FORM>
</BODY>
</HTML>
Can anyone help me out?
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
Justen,
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.


Matthew Phillips
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Justen
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


Dave
 
wood burning stoves
 
subject: Can Someone help me out?
 
Similar Threads
Help me fix this-Part-2,so I can compile(generics doubt)
Generics problem- why not taking Animal, Object and super of Dog
Casting to an Interface Type
Inheritance, overriding methods
Help me fix this,so I can compile(generics doubt)