package voteCounterwebapp;
// VoteCounter.java
// This servlet processes the ballot form, returning a
// page asking for a new vote if no vote was made on the
// ballot. For legitimate ballots, the vote is added to
// the current totals, and those totals are presented to
// the user in a return page.
// A cookie is returned to the voter, recording the fact
// that a vote was received. The servlet examines all votes
// for cookies to ensure that there is no multiple voting.
// The voting data file, votesdat.dat, is stored on the Web server.
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class VoteCounter extends HttpServlet {
Cookie cookies[] = null;
int index;
PrintWriter servletOut;
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
Cookie newCookie;
int votes[] = null;
String vote;
File votesdat = new File("votesdat.dat");
String candidates[] = {"Daren Dogman", "Timmy Taildragger",
"Don Dogpile"};
// Get cookies from the request
cookies = request.getCookies();
/ If the file already exists, read in its data
if (votesdat.exists()) {
ObjectInputStream indat = new ObjectInputStream(
new FileInputStream(votesdat));
// We need the try/catch here because readObject can throw
// ClassNotFoundException
try {
votes = (int []) indat.readObject();
}
catch(ClassNotFoundException problem) {
problem.printStackTrace();
}
indat.close();
}
// If the file does not exist (this is the first vote), create the
// votes array
else {
votes = new int[3];
}
// Check to see if there was a vote on the form
vote = request.getParameter("vote");
if (vote == null) { // There was no vote
// Create the return page
makeHeader(response);
servletOut.println(
"You submitted a ballot with no vote marked
");
servletOut.println(
"Please mark the ballot and resubmit");
} // end of if (vote == null)...
else { // There was a vote
// Check to see if this client voted before
if (!votedBefore()) {
// No previous vote, so add the new vote of the response
// to the votes array
if (vote.equals("Dogman"))
votes[0]++;
else if (vote.equals("Taildragger"))
votes[1]++;
else votes[2]++;
// Write updated votes array to disk
ObjectOutputStream outdat = new ObjectOutputStream(
new FileOutputStream(votesdat));
outdat.writeObject(votes);
outdat.flush();
outdat.close();
// Attach a cookie to the response
newCookie = new Cookie("iVoted", "true");
newCookie.setMaxAge(5);
response.addCookie(newCookie);
// Write a response message
makeHeader(response);
servletOut.println("Your vote has been received");
servletOut.println(
"
Current Voting Totals:
");
// Create the total votes return information
for (index = 0; index < 3; index++) {
servletOut.println("
");
servletOut.print(candidates[index]);
servletOut.print(": ");
servletOut.println(votes[index]);
}
} // end of if (!votedBefore() ...
else { // The client voted before
// Write a response message
makeHeader(response);
servletOut.println(
"Your vote is illegal - you have already voted!");
} // end of else clause - client voted before
} // end of else (there was a vote)
// Finish response document and close the stream
servletOut.println("