📄 votecounter2.java
字号:
// VoteCounter2.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.
// This servlet uses session tracking rather than cookies.
// The voting data file, votesdat.dat, is stored on the Web server.
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class VoteCounter2 extends HttpServlet {
PrintWriter servletOut;
int index;
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
HttpSession mySession = null;
int votes[] = null;
String vote;
File votesdat = new File("votesdat.dat");
String candidates[] = {"Daren Dogman", "Timmy Taildragger",
"Don Dogpile"};
// 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(mySession, request)) {
// No previous vote, so read the file (if it exists)
// Synchronize a block for the file access
synchronized(this) {
if (votesdat.exists()) {
ObjectInputStream indat =
new ObjectInputStream(
new FileInputStream(votesdat));
// We need try/catch here because readObject may throw the
// ClassNotFoundException
try {
votes = (int []) indat.readObject();
}
catch(ClassNotFoundException problem) {
problem.printStackTrace();
}
indat.close();
} // end of if (votesdat...
// If the file does not exist (this is the first vote), create the
// votes array
else
votes = new int[3];
// 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();
} //** end of the synchronized block
// Create a session object and set a value to indicate a vote
mySession = request.getSession(true);
mySession.putValue("iVoted", "true");
// Write a response message
makeHeader(response);
servletOut.println("Your vote has been received");
servletOut.println(
"<br/> <br/> Current Voting Totals:<br/>");
// Create the total votes return information
for (index = 0; index < 3; index++) {
servletOut.println("<br/>");
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 already voted!");
} // end of else
} // end of else (there was a vote)
servletOut.println("</body> </html>");
servletOut.close();
} // end of doPost
//----------------------------------------------------------------
// Method votedBefore - return true if the client voted before;
// false otherwise
boolean votedBefore(
HttpSession mySession, HttpServletRequest request) {
// Get the session object, if there is one
mySession = request.getSession(false);
// If there was no session, the vote must be okay
if (mySession == null)
return false;
else { // there was a session
String names [] = mySession.getValueNames();
for (index = 0; index < names.length; index++) {
if (names[index].equals("iVoted") &&
mySession.getValue(names[index]).equals("true"))
return true;
} // end of for (index = 0; ...
return false;
} // end of else
} // end of votedBefore
//----------------------------------------------------------------
// Method makeHeader -
// get the writer and produce the response header
void makeHeader(HttpServletResponse response)
throws IOException {
// Set content type for response and get a writer
response.setContentType("text/html");
servletOut = response.getWriter();
// Write the response document head and the message
servletOut.println("<html><head>");
servletOut.println(
"<title> Return message - </title></head><body>");
} // end of makeHeader
} // end of VoteCounter2
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -