views:

40

answers:

2

I know this has been asked before, but it just doesn't work for me.

I'm trying to populate a drop down list from a database using jstl, here's the class i use as a usebean:

public class feedData {

    Connection con;

    public feedData() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {

        Class.forName("com.mysql.jdbc.Driver").newInstance();
        String url = "******";

        con = DriverManager.getConnection(url, "**", "**");

    }

    public ArrayList<String> getUnis() throws SQLException {
        ArrayList<String> uniList = new ArrayList<String>();
        String tryquery = "select aff from libra.smalluniqdbtmp";
        Statement stmt2 = con.createStatement();
        ResultSet rs1 = stmt2.executeQuery(tryquery);

        while (rs1.next()) {

            uniList.add(rs1.getString("aff"));

        }

        return uniList;
    }

    public ArrayList<String> getRI() throws SQLException {
        ArrayList<String> RIList = new ArrayList<String>();
        String tryquery = "select interest from libra.riuniqdb";
        Statement stmt2 = con.createStatement();
        ResultSet rs1 = stmt2.executeQuery(tryquery);

        while (rs1.next()) {

            RIList.add(rs1.getString("aff"));

        }

        return RIList;
    }
}

Here's my jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd"&gt;

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>

        <jsp:useBean id="obj" scope="page" class="uiLibraWeb2Pkg.feedData" />
        <h1>Hello World!</h1>
        <table border="1">
            <thead>
                <tr>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td> <form action="response.jsp">
                            <strong>Select a university:</strong>

                            <select name="affiliation">
                                <c:forEach var="aff" items="${obj.unis}">
                                    <option value="${aff}"></option>
                                </c:forEach>

                            </select>

                        </form>

                    </td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
                <tr>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
            </tbody>
        </table>

    </body>
</html>

There are no error messages in the server log and in the build output of the project, but the drop down list is empty. Been struggling with this, and I have no clue as to what's wrong. I also tried to set a data source and do it using jstl iterating through the resultset of the query, but that acted the same way.

Now i'm not relying on any data source, but still the same results.

Help appreciated, Thanks

A: 

Rightclick the HTML page in webbrowser and View Source. Big chance that you see the JSTL tags unparsed in there. That is right, you didn't import the JSTL taglib in top of JSP as per the JSTL core taglib documentation:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

(and further also don't forget to set the option label as mentioned by @Guido)

If that doesn't fix the problem and you get compilation errors of unrecognized taglib, then it means that the servletcontainer in question doesn't ship with JSTL and that you'll have to install JSTL yourself. It's basically simple: just drop jstl-1.2.jar in /WEB-INF/lib of your webapp and redeploy.

If you ever wonder why that's not needed for JSP tags, well, that's because they are by default already recognized by the JspServlet. Any other taglibs should be declared explicitly as @taglib in top of JSP.


Unrelated to the actual problem: your JDBC code doesn't look right. You're not closing connections and thus leaking resources. If you run this repeatedly over a long period, then the DB will run out of connections and your application will break. You may find this article useful to learn how to use JDBC code properly.

BalusC
Thank you, the taglib you are referring to was in the code. It got deleted when I was removing some comments from the code. My bad, can't expect people to help when i make such mistakes. Thanks for the JDBC article, i'll look it up.
A: 

I have not tested your code, but please add a breakpoint in the server side to check how many elements you have in the list. Maybe it is also empty in the server side.

The second point is that this code

<option value="${aff}"></option>

doesn't show anything to the user, because it is rendered in HTML as an option with no text. Maybe it should be

<option value="${aff}">${aff}</option>
Guido
Thanks for the suggestion, that worked. Stupid of me.