question archive Revise the survey sample servlet Survey

Revise the survey sample servlet Survey

Subject:Computer SciencePrice:2.89 Bought4

Revise the survey sample servlet Survey.java to record the number of votes so far in the data file and then display that count every time a vote is submitted or a survey result is requested. Also, change the output table so that its data is a percentage of the total votes for the particular gender category.

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

<%-- 

 Document : index - for the solution to Exercise 11.4

 Created on : May 24, 2008, 8:37:33 PM

 Author : bob

--%>

 

<%@page contentType="text/html" pageEncoding="UTF-8"%>

 

 

 

 

 content="text/html; charset=UTF-8">

 

 

 

 

 

 

 

 

 Welcome to the Consumer Electronics Purchasing Survey

 

 

 

 

 

 

Your Gender:

 

 

 

 

 

 checked="checked" />

 Female

 

  

 

 Male

 

 

 

 

 

 

 

 

 TV

 

 

 

 Digital Camera

 

 

 

 MP3 player

 

 

 

 DVD player/recorder

 

 

 

 Camcorder

 

 

 

 PDA

 

 

 

 checked="checked" />

 Other
 

 

 

 

 

 

 

 

 

 

 

 

/* e114 - Servlet for Exercise 11.4 */

 

import java.io.*;

import java.net.*;

import javax.servlet.*;

import javax.servlet.http.*;

 

public class Survey extends HttpServlet {

 protected void processRequest(HttpServletRequest request, 

 HttpServletResponse response)

  throws ServletException, IOException {

 int votes[] = null;

 int index;

 int sumFemales, sumMales;

 int vote;

 File survdat = new File("survdat.ser");

 String gender;

 String products[] = {" TV", "Digital Camera", "MP3 player",

 "DVD player/recorder", "Camcorder", "PDA", Other"};

 

 // Set the content type for the response output and get a

 // writer

 response.setContentType("text/html;charset=UTF-8");

 PrintWriter out = response.getWriter();

 

 // Create the initial part of the response document

 out.println("");

 out.println("");

 out.println("");

 out.println("");

  out.println("");

 

 // Synchronize a block for the votes file access

 synchronized (this) {

 

 // 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[15];

  votes[14] = 0;

 }

 } catch (Exception e) {

 e.printStackTrace();

 }

 

 // Get the gender of the survey respondee

 gender = request.getParameter("gender");

 

 // Increment the visits counter

 votes[14]++;

 

 // Add the consumer electronics vote of the response to

 // the votes array

 vote = Integer.parseInt(request.getParameter("vote"));

 if (gender.equals("male")) {

 vote += votes.length / 2;

 }

 votes[vote]++;

 

 //Write updated votes array to disk

 ObjectOutputStream outdat = new ObjectOutputStream(

 new FileOutputStream(survdat));

 outdat.writeObject(votes);

 outdat.flush();

 outdat.close();

 

 } //** end of the synchronized block

 

 // Create the initial response information

 out.println(

 "

Thank you for participating in the");

 

 out.println(" Consumer Electronics Survey

");

 

 out.println("You are visitor number " + votes[14] + 

 "
");

 out.println("

Current Survey Results:

");

 

 

 // Create the total votes return information for female

 // respondents

 out.println("

For Female Respondents

");

 

 

 // First, sum the votes for females and males

 sumFemales = 0;

 sumMales = 0;

 for (index = 0; index < votes.length / 2; index++) {

 sumFemales += votes[index];

  sumMales += votes[index + votes.length / 2];

 }

 for (index = 0; index < votes.length / 2; index++) {

 out.print(products[index]);

 out.print(": ");

 if (sumFemales > 0) {

  out.print((100 * votes[index]) / sumFemales);

 out.println("%");

 }

 else

 out.println(0);

 out.println("
");

 }

 

 // Create the total votes return information for male

 // respondents

 out.println("

For Male Respondents

"); for (index = votes.length / 2; index < votes.length; index++) { out.print(products[index - votes.length / 2]); out.print(": "); if (sumMales > 0) {out.print((100 * votes[index]) / sumMales); out.println("%"); } else out.println(0);out.println(" ");}out.close();}protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); }}