• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can Someone help me out?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
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?
 
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic