package servlets; import java.io.*; import java.text.DecimalFormat; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; /** * Implements a survey of favorite pets */ public class SurveyServlet extends HttpServlet { private static final long serialVersionUID = 1L; private static final String animalNames[] = { "dog", "cat", "bird", "snake", "fish", "other", "none" }; /** * @see HttpServlet#HttpServlet() */ public SurveyServlet() { super(); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int votesForAnimals[] = null; int totalVotes = 0; String userChoice = request.getParameter("animal"); // now send a thanks to the client and the results response.setContentType("text/html"); String docType = ""; String html = "\n"; // get the output stream for the client PrintWriter clientOutput = response.getWriter(); // generate the response document clientOutput.println(docType); clientOutput.println(html); // Error: return an error response to the user if (userChoice == null) { clientOutput.println("Error!"); clientOutput.println(""); clientOutput.println("

Ooops!

"); clientOutput.println("

Error: Please submit a choice for animal.

"); clientOutput.println("

Return to Pet Survey

"); clientOutput.println("\n"); clientOutput.close(); return; } // read in the survey results so far File surveyData = new File("survey.results"); if (surveyData.exists()) { try { ObjectInputStream in = new ObjectInputStream(new FileInputStream(surveyData)); votesForAnimals = (int[]) in.readObject(); in.close(); for (int x = 0; x < votesForAnimals.length; x++) totalVotes += votesForAnimals[x]; } catch (ClassNotFoundException exp) { System.err.println("Error reading in file" + surveyData); } } else { votesForAnimals = new int[animalNames.length]; } // read current response (that caused this to run) totalVotes++; // determine which was selected and update the total for (int x = 0; x < animalNames.length; x++) { if (userChoice.equals(animalNames[x])) { votesForAnimals[x]++; break; } } // write updated total to the file ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(surveyData)); out.writeObject(votesForAnimals); out.flush(); out.close(); // determine percentages double percentages[] = new double[votesForAnimals.length]; for (int x = 0; x < percentages.length; x++) percentages[x] = 100.0 * votesForAnimals[x] / totalVotes; clientOutput.println("Thank You!"); clientOutput.println(""); clientOutput.println("

Thank you for your input!

"); clientOutput.println("

Results:

"); DecimalFormat twoDigits = new DecimalFormat("#0.00"); clientOutput.println(""); for (int x = 0; x < percentages.length; x++) { clientOutput.println(""); } clientOutput.println("
"); clientOutput.println(animalNames[x] + ""); clientOutput.println(twoDigits.format(percentages[x])); clientOutput.println("" + votesForAnimals[x] + "
"); clientOutput.println("

Total Responses: "); clientOutput.println(totalVotes + "

"); clientOutput.println("\n"); clientOutput.close(); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }