Drop down menu not populating, where is my error?

helloApp :

So, I have struggled for 2 days to understand what my error is. My drop down list is not populating with data from the database. I am using Java EE and Mysql.

I can insert data in the database without a problem but when i retrieve it for some reason the jsp doesn't do its thing correctly so that I always get the drop down empty. This is the table I am using :

    category_id int auto_increment,
    name varchar(30),
        primary key(category_id)
        );

Rhis is the servlet I am using and the method for executing the query. As you can see I am returning a List of objects and then I am adding it as a request attribute in the doGet method. So I guess until here everything should work fine.

public class DropDownServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        try {
            List<Category> categories = retrieveCategories();
            request.setAttribute("categories", categories);
            RequestDispatcher view = request.getRequestDispatcher("viewdropdown.jsp");
            view.forward(request, response);
        } catch (ClassNotFoundException | SQLException | IOException ex) {
            ex.printStackTrace();
        }
    }

    public static List<Category> retrieveCategories() throws ClassNotFoundException, SQLException {
        Connection conn = DatabaseConnection.initializeConnection();
        String query = "Select * from category";
        PreparedStatement pstmnt = conn.prepareStatement(query);
        List<Category> categories = new ArrayList<>();


        ResultSet rs = pstmnt.executeQuery();

        while (rs.next()) {

            int id = rs.getInt("category_id");
            String name = rs.getString("name");

            Category cat = new Category(id,name);
            categories.add(cat);

        }

        conn.close();
        return categories;
    }

and here is the jsp with the dropdown wich doesn't display the data that it should.

<html>

<head>
    <title>Dropdown page</title>
</head>

<body>
<h1>The names of the categories are the following:</h1>

<select id ="dropdown">
    <c:forEach items="${categories}" var="category">
        <option value = "${category.name}">${category.name}</option>
    </c:forEach>
</select>
    <br>
    <br>

</body>
</html>

EDIT: So i have created this hardcoded test method to test my code and i add it as request parameter identically to how i added the other method with the DB query.My DropDown is still empty, so the problem could be from the way i try to display the items in the jsp.I think it could be in the loop i have.

public static List<Category> testMethod(){
        List<Category> list = new ArrayList<>();
        Category one = new Category(1,"Blue");
        Category two = new Category(2,"Red");
        list.add(one);
        list.add(two);

        return list;

    }
ptr92zet :

Looks like the ${categories} is an empty collection.

First, make sure, you have imported the JSTL Core library into your JSP page. This is done by putting

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

at the top of the JSP page.

Additionally, you can check on the JSP side if the collection is empty by adding somewhere:

<c:out value="${categories.size()}" />.

Also, you are using Expression Language directly between the HTML <option> tags, so you will also need adding:

<%@ page isELIgnored = "false" %>

at the top of the JSP page to make it evaluable to the appropriate value, instead of rendering plain text.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=169737&siteId=1