/* * Copyright (c) 2001, Xiaoping Jia. * All Rights Reserved. */ package db; import java.io.*; import java.util.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; /** * A servlet that counts the votes for the Best Pictures of the 2001 Acadamy Awards, and * for the favorate pictures. * This version uses only JDBC 1.0 features. Another version of the program, * db.Oscar2, uses JDBC 2.0 features, which requres a JDBC 2.0 compliant driver. * * The votes are stored in a database, which is created by a separate application * OscarDB. The database must be created before the Oscar servlet is started * for the first time. * * @version 1.1 2001/04/29 * @since 1.0 * @author Xiaoping Jia * @see db.OscarDB * @see db.Oscar2 */ public class Oscar extends HttpServlet { public void init() { System.out.println("In Oscar.init()..."); ServletContext ctx = getServletContext(); DBConnector.setDBPropertiesFileName(ctx.getRealPath("WEB-INF/classes/db.properties")); DBConnector db = DBConnector.getInstance(); db.setHomeDir(ctx.getRealPath("WEB-INF/classes/")); try { db.startup(OscarDB.dbName); } catch(ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } public void destroy() { System.out.println("In Oscar.destroy()..."); DBConnector db = DBConnector.getInstance(); db.shutdown(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("In Oscar.doPost()..."); // get the reques parameters String oscar = request.getParameter("oscar"); String title1 = request.getParameter("first"); String title2 = request.getParameter("second"); String title3 = request.getParameter("third"); // get prepared to write results response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(""); out.println(" Oscar: Best Picture"); out.println(""); out.println("

Your vote among the nominated pictures

"); // print out what user submitted if (oscar != null) { if ("Brockovich".equals(oscar)) { out.println("Erin Brockovich"); } else if ("Dragon".equals(oscar)) { out.println("Crouching Tiger, Hidden Dragon"); } else { out.println(oscar); } } else { out.println("None."); } // print out cumulative results out.println("

Top 3 picks of your favorate pictures of the year

"); out.println(""); out.println(""); out.println(""); out.println(""); out.println("
First:" + title1 + "
Second:" + title2 + "
Third:" + title3 + "
"); out.println("
"); out.println("

Voting results

"); try { long t1 = System.currentTimeMillis(); DBConnector db = DBConnector.getInstance(); Connection conn = db.getConnection(); Statement stmt = conn.createStatement(); long t2 = System.currentTimeMillis(); String queryString; String updateString = null; out.println("

Best picture

"); out.println(""); queryString = "SELECT * FROM " + OscarDB.bestPictureTableName; ResultSet rset = stmt.executeQuery(queryString); while (rset.next()) { String id = rset.getString("id"); String title = rset.getString("title"); int count = rset.getInt("count1"); if (oscar != null && oscar.equals(id)) { updateString = "UPDATE " + OscarDB.bestPictureTableName + " SET count1=" + ++count + " WHERE id='" + id + "'"; } out.println(""); } rset.close(); if (updateString != null) { stmt.executeUpdate(updateString); } out.println("
" + title + "" + count + "
"); String id1 = normalize(title1); String id2 = normalize(title2); String id3 = normalize(title3); if (id1 != null) { queryString = "SELECT * FROM " + OscarDB.favoratePictureTableName + " WHERE id = '" + id1 + "'"; rset = stmt.executeQuery(queryString); updateString = null; if (rset.next()) { int count1 = rset.getInt("count1st"); updateString = "UPDATE " + OscarDB.favoratePictureTableName + " SET count1st=" + ++count1 + " WHERE id='" + id1 + "'"; } else { updateString = "INSERT INTO " + OscarDB.favoratePictureTableName + " VALUES ('" + id1 + "', '" + title1 + "', 1, 0, 0)"; } rset.close(); if (updateString != null) { stmt.executeUpdate(updateString); } } if (id2 != null) { queryString = "SELECT * FROM " + OscarDB.favoratePictureTableName + " WHERE id = '" + id2 + "'"; rset = stmt.executeQuery(queryString); updateString = null; if (rset.next()) { int count2 = rset.getInt("count2nd"); updateString = "UPDATE " + OscarDB.favoratePictureTableName + " SET count2nd=" + ++count2 + " WHERE id='" + id2 + "'"; } else { updateString = "INSERT INTO " + OscarDB.favoratePictureTableName + " VALUES ('" + id2 + "', '" + title2 + "', 0, 1, 0)"; } rset.close(); if (updateString != null) { stmt.executeUpdate(updateString); } } if (id3 != null) { queryString = "SELECT * FROM " + OscarDB.favoratePictureTableName + " WHERE id = '" + id3 + "'"; rset = stmt.executeQuery(queryString); updateString = null; if (rset.next()) { int count3 = rset.getInt("count3rd"); updateString = "UPDATE " + OscarDB.favoratePictureTableName + " SET count3rd=" + ++count3 + " WHERE id='" + id3 + "'"; } else { updateString = "INSERT INTO " + OscarDB.favoratePictureTableName + " VALUES ('" + id3 + "', '" + title3 + "', 0, 0, 1)"; } rset.close(); if (updateString != null) { stmt.executeUpdate(updateString); } } out.println("

Favorite pictures

"); out.println(""); out.println(""); queryString = "SELECT * FROM " + OscarDB.favoratePictureTableName + " ORDER BY count1st DESC, count2nd DESC, count3rd DESC"; rset = stmt.executeQuery(queryString); while (rset.next()) { String id = rset.getString("id"); String title = rset.getString("title"); int count1 = rset.getInt("count1st"); int count2 = rset.getInt("count2nd"); int count3 = rset.getInt("count3rd"); out.println(""); } rset.close(); out.println("
TitleFirstSecondThird
" + title + "" + count1 + "" + count2 + "" + count3 + "
"); stmt.close(); conn.close(); long t3 = System.currentTimeMillis(); System.out.println("Make DB connection: " + (t2 - t1) + "ms"); System.out.println("Total DB access/processing: " + (t3 - t1) + "ms"); } catch (SQLException se) { se.printStackTrace(); } out.println(""); out.println(""); } protected Set stopWords; // initialization block { stopWords = new HashSet(); stopWords.add("a"); stopWords.add("an"); stopWords.add("and"); stopWords.add("for"); stopWords.add("of"); stopWords.add("the"); stopWords.add("with"); } protected String normalize(String title) { if (title != null) { title = title.trim(); if (title.length() > 0) { StringTokenizer st = new StringTokenizer(title); StringBuffer sb = new StringBuffer(); boolean first = true; while (st.hasMoreTokens()) { String word = st.nextToken(); word = word.trim().toLowerCase(); if (!stopWords.contains(word)) { if (!first) { sb.append(' '); } else { first = false; } sb.append(word); } } return sb.toString(); } } return null; } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("In Oscar.doGet()..."); doPost(request, response); } }