0

First off, I want to avoid the use of scriptlets and/or outside libraries. What I'm making is an online polling system, where users can register, log in, and vote on a particular question. That vote is registered and stored in the database. The problem I'm facing is that after I'm retrieving the polls from the Database, I don't know the best way of displaying them in a .jsp page. Again, I want to avoid the use of scriptlets.

Only an "administrator" can create/remove polls. I have a servlet that handles this:

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    Database db = new Database();
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String question = request.getParameter("question");
    String o1 = request.getParameter("o1");
    String o2 = request.getParameter("o2");
    String o3 = request.getParameter("o3");
    String o4 = request.getParameter("o4");
    String o5 = request.getParameter("o5");

    if (question == null || o1 == null || o2 == null) {
        request.getRequestDispatcher("new.jsp").include(request, response);
        out.println("<br>");
        out.println("* Poll must have a question and at least 2 choices.");
    } else {
        int id = (int) System.currentTimeMillis();
        db.createPoll(id, question, o1, o2, o3, o4, o5);
        request.getRequestDispatcher("new.jsp").include(request, response);
        out.println("<br>");
        out.println("Poll created.");
    }
}

My createPoll method from Database class:

    // Method for creating polls. The information is added to the database.
public void createPoll(int id, String question, String o1, String o2, String o3, String o4,
        String o5) {

    try {

        Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
        connection = DriverManager
                .getConnection("jdbc:derby:C:\\MyDB;create=true;upgrade=true");
        String query = "INSERT INTO Polls (id, question, o1, o2, o3, o4, o5) VALUES"
                + "(?,?,?,?,?,?,?)";
        preparedStatement = connection.prepareStatement(query);
        preparedStatement.setInt(1, id);
        preparedStatement.setString(2, question);
        preparedStatement.setString(3, o1);
        preparedStatement.setString(4, o2);
        preparedStatement.setString(5, o3);
        preparedStatement.setString(6, o4);
        preparedStatement.setString(7, o5);
        preparedStatement.executeUpdate();

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        close();
    }
}

How I retrieve all the data from the Polls table:

// This method retrives all the polls in the database
// and stores them in an HashMap for further processing
public HashMap<String, ArrayList<String>> getPolls() {

    HashMap<String, ArrayList<String>> polls = new HashMap<String, ArrayList<String>>();

    try {

        Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
        connection = DriverManager
                .getConnection("jdbc:derby:C:\\MyDB;create=true;upgrade=true");
        String query = "SELECT * from Polls";
        preparedStatement = connection.prepareStatement(query);
        resultSet = preparedStatement.executeQuery();

        while (resultSet.next()) {
            String id = String.valueOf(resultSet.getInt("id"));
            String question = resultSet.getString("question");
            String o1 = resultSet.getString("o1");
            String o2 = resultSet.getString("o2");
            String o3 = resultSet.getString("o3");
            String o4 = resultSet.getString("o4");
            String o5 = resultSet.getString("o5");
            polls.put(id, new ArrayList<String>(Arrays.asList(question, o1, o2, o3, o4, o5)));
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        close();
    }
    return polls;
}

So at the end, I'm left with a HashMap containing all the data from Polls. I want these to be displayed in a jsp page. Question first, then all the choices as radio buttons. What is best way of achieving this?

Apologize for the long post!

0 Answers0