// Survey.java // This servlet processes the consumer electronics survey // form, updating the file that stores the survey data // and producing the current total votes in the survey. // The survey data file, survdat.dat, is stored on the Web server. import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class Survey extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int votes[] = null; int index; int vote; File survdat = new File("survdat.dat"); String gender; String products[] = {"Conventional TV", "HDTV", "VCR", "CD Player", "Mini CD player/recorder", "DVD player", "Other"}; // Set the content type for the response output and get a writer response.setContentType( "text/html" ); PrintWriter servletOut = response.getWriter(); // Produce the head of the output document servletOut.println(""); servletOut.println( " Return message "); // If the file already exists, read in its data try { if (survdat.exists()) { ObjectInputStream indat = new ObjectInputStream( new FileInputStream(survdat)); votes = (int []) indat.readObject(); indat.close(); } // If the file does not exist (this is the first vote), create the // votes array else votes = new int[14]; } catch (Exception e) {e.printStackTrace();} // Get the gender of the survey respondee gender = request.getParameter("gender"); // Add the consumer electronics vote of the response to the // votes array vote = Integer.parseInt(request.getParameter("vote")); if (gender.equals("male")) vote += 7; votes[vote]++; // Write updated votes array to disk ObjectOutputStream outdat = new ObjectOutputStream( new FileOutputStream(survdat)); outdat.writeObject(votes); outdat.flush(); outdat.close(); // Create the initial response information servletOut.println( "

Thank you for participating in the"); servletOut.println(" Consumer Electronics Survey

"); servletOut.println("

Current Survey Results:

"); // Create the total votes return information for female respondents servletOut.println("
For Female Respondents
"); for (index = 0; index < 7; index++) { servletOut.print(products[index]); servletOut.print(": "); servletOut.println(votes[index]); servletOut.println("
"); } // Create the total votes return information for male respondents servletOut.println("
For Male Respondents
"); for (index = 7; index < 14; index++) { servletOut.print(products[index - 7]); servletOut.print(": "); servletOut.println(votes[index]); servletOut.println("
"); } servletOut.close(); } }