JSP Expression Language not rendering properties from JavaBean

Viewed 627

Well, this error is surely caused by making some sort of mistake done by me. Here is my code below.

The servlet:

package Servlets;

// import statements

public class AdminResource extends HttpServlet {

    List<userList> users = new ArrayList<>();

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
                                throws ServletException, IOException {
        Database db = (Database) getServletContext().getAttribute("db");
        String sql = "select * from TAHMID_NIPS.GROUPS";
        ResultSet rs;
        try {
            rs = db.runSql(sql);
            // int i = 0;
            while (rs.next()) {
                userList user = new userList(rs.getString("USERNAME"), rs.getString("GROUPID"));
                users.add(user);
                /* used a printWriter here to access the bean in the servlet
                out.println(users.get(i).getUserName());
                out.println(users.get(i).getGroupID());
                i++;
                */
            }
            request.setAttribute("users", users);
            request.getRequestDispatcher("userList.jsp").forward(request, response);
        } catch(SQLException ex){}
    }

    // doGet(), doPost(),  getServletInfo() methods..
}

The JavaBean class::

public class userList {

    private String GroupID;
    private String UserName;

    public userList(String GroupID, String UserName){
        this.GroupID = GroupID;
        this.UserName = UserName;
    }

    public String getGroupID() {
        return GroupID;
    }

    public void setGroupID(String GroupID) {
        this.GroupID = GroupID;
    }

    public String getUserName() {
        return UserName;
    }

    public void setUserName(String UserName) {
        this.UserName = UserName;
    }
}

The dispatched JSP view:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <h1>These are the users in the database!</h1><br>
    <c:forEach var="iterator" items="${users}">
        <c:out value="${iterator.UserName}"/> <br>
    </c:forEach>
</body>
</html>

The database URL, passwords and other required information are declared in the web.xml file and a contextListener class is implemented. The app was working fine when data was being shown in the servlet. But as we are MVC fans, problem started when I tried to access the bean with EL. The fields were unable to be accessed.

About the database:
Here is a table named Groups with two fields: UserName, GroupID. But none of them are being shown in the JSP view.

The problem might have occurred in the EL in the JSP view. Experts, please help.

1 Answers
Related