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?