Back to Dot-Com Builder How-Tos Archive
Developing Polls and Surveys
by Carla King You've been charged with putting a poll in place for your company's Web site. How you do this, of course, will depend on the technology you're currently using, and its restrictions. For example, you can't run a serious, scientifically bulletproof poll if you're using run-time memory or flat file to store votes instead of a database. This article discusses the mechanics of building a simple poll (such as the one shown on the Dot-Com Builder Web site), which includes the following tasks:
Writing This Application Your system is the biggest factor for determining what kind of polling is possible on your site. If you're creating a poll to use with systems based on Java technology, you'll be working with Java servlets and JSP pages, which provide you with a mature, object-oriented application development platform with hooks to the entire library of Java technology features and integration capabilities.You can use other technologies, too, such as PHP and Perl, to achieve similar speed and scalability. You probably already understand the limitations of the technology you have in place now, and you may need to provide a reality check for those in your company who would like to implement polls beyond your system's capabilities. If your site is mostly static -- with light database traffic -- adding a poll to your front page can really burden your infrastructure, and you may actually need to advise against it. Here are some options for writing your polling application. You could:
Once voting is complete, polling results are usually displayed to users in numbers or in graph or bar charts. Because content developers will need to access this formatting code, it's important to keep the formatting code of the results away from the code used to implement the poll. Data Storage and State Database and flat-file solutions account for the bulk of polling solutions, with about 95 percent of solutions using a database. If you have no storage capabilities, you can hold the results in run-time memory only until your server is shut down, so your poll can only be used for entertainment purposes. Issues surrounding each data storage option, with comments on state, are explained in the following:
Database Storage If you are working with a database system, you'll use either a GUI or SQL scripts to load the data into the database. Your Web GUI will determine the questions, possible answers, and how to show results. Most server technologies allow you to interface with a database, which is the best solution if:
With a database you can use a connection pool so that connections to the database are not opened every time there's a vote. This reduces the voting overhead on your systems and increases the level of concurrency by providing multiple connections at a time -- allowing many people to vote simultaneously -- which speeds up the voting process. Since connection pools need persistence, this is much more easily accomplished using JSP pages and servlets with built-in persistence. Flat-File Data Storage If you're creating a poll using flat files, you can manually input the questions and possible answers of the poll in the flat files or write a GUI Web-based application to do it. If you need to keep the data, you can use server-side code to direct the data to be stored in flat files on your operating system. You can use any technology you wish for this task. If your site attracts a lot of visitors, locked flat files or overwritten votes due to more than one visitor voting at the same time could be a problem, and you'll have to provide the coding to keep this from happening. However, this situation would rarely occur on low traffic sites. No Data Storage Because the Web is stateless, you must implement state in order for your system to count votes and to remember who voted. Servlet engines allow for application-wide persistent data, which is stored for the entire polling application until the Web server is shut off. Therefore, you'll need to write application code that will create a new poll when the server starts up again. If you're using Java servlets or JSP pages, you can use their available, application-wide persistence storage facilities to keep track of votes. If you need to store votes for the long term, you should use a more permanent method of data persistence. Handling Ballot Stuffing To prevent a visitor from voting more than once you can:
The pros and cons of each are discussed in the next sections.
Cookies
public String parsePoll(int total_votes,
Vector answers, String entry_html_file)
{
ParsedFileBuffer entry =
new ParsedFileBuffer(new
FileInputStream(entry_html_file));
Map subs = new HashMap();
StringBuffer buf = new StringBuffer();
for (int idx = 0; idx <
answers.size(); idx++) {
String[] record = (String [])
answers.elementAt(idx);
// make sure the record has only
// two entries
if (record.length == 2 && record[0]
!= null &&
!record[0].equals("")) {
String answer = record[0];
String votes = record[1];
int ratio =
Integer.parseInt(votes) *
1000 / total_votes;
subs.put("answer", answer);
subs.put("votes", votes);
subs.put("percent",
Integer.toString(ratio / 10));
subs.put("percent_real",
Double.toString((double) ratio /10.0));
buf.append(entry.getParsedContents(subs))
.append("\n");
}
}
return buf.toString();
}
Figure 1: Preventing ballot stuffing using the Servlet Sessions
IP Logging
Displaying Results There are several ways to display the poll tally. Each is suited better or worse for a specific task. Here are some pros and cons for the different ways they can be displayed. Pop-up Window
Java Applet
Page Reload
Code Snippet
The
The
public void service(HttpServletRequest req,
HttpServletResponse res)
throws IOException, ServletException
{
String[] values =
req.getParameterValues("pollId");
//
// check that we got a valid poll id
//
if (values == null) {
res.sendError(400, "invalid poll id");
return;
}
String pollId = values[0];
boolean pollAlreadyTaken = false;
String pollsTakenList = null;
//
// get the list of polls already taken.
Cookie existing =
HttpUtils.getCookie("POLLS_TAKEN", req);
if (existing != null) {
//
// walk through the list and see if they
// have already taken this poll.
StringTokenizer st =
new StringTokenizer(existing.getValue(),
",");
while (st.hasMoreTokens()) {
String value = st.nextToken();
if (value.equals(pollId)) {
// the user has already taken this poll.
pollAlreadyTaken = true;
break;
}
}
//
// If the user hasn't already taken the poll,
// add the id to the already taken list.
if (!pollAlreadyTaken)
pollsTakenList = existing.getValue()
+ "," + pollId;
} else {
pollsTakenList = pollId;
}
//
// If the user has not already taken this poll,
// update the POLLS_TAKEN cookie
if (!pollAlreadyTaken) {
Cookie cookie = new Cookie(_cookiename,
pollsTakenList);
cookie.setPath("/");
res.addCookie(cookie);
recordVote(req);
}
}
Figure 2: Outputting the poll results using Acknowledgments Phil Bartholo is a software developer specializing in server-side Java technology. He has written for Dot-Com Builder, java.sun.com, and developer.iplanet.com. Kito D. Mann is cofounder of Virtua Communications Corp., the developer of FastVote, a Java technology-based Web survey system. Joe Mocker is the lead developer for Dot-Com Builder, Java Developer Connection, and Solaris Developer Connection. He designed the polling and survey system used on these sites. Resource
|
| ||