4

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.

informatik01
  • 16,038
  • 10
  • 74
  • 104
Tahmid Ali
  • 805
  • 9
  • 29
  • 2
    See [similar question](http://stackoverflow.com/questions/12000557/how-to-link-a-jsp-servlet-and-a-java-view-bean). You actually have to pass the bean to the JSP in some way (say as a request or session attribute), before you can call it on EL. Also, best to use [JSTL](http://stackoverflow.com/a/3445373/136363) instead of plain EL for better security. – t0mppa Dec 31 '13 at 20:25
  • The easiest way to get your code to work is to change a line in your Servlet AdminResource. Change model.list(context); to request.setAttribute("users", model.list(context)); – rickz Jan 01 '14 at 03:25
  • @t0mppa I changed the servlet code and removed the query provider java class according to the similar question link you provided. Then I was able to access the bean in the servlet with the commented code section in the servlet. But still can't get the records viewed in the JSP. Is there an equivalent of users.get(i).getUserName() in the JSP EL? Or is there any modification that I need to make? Please help. And thanks for the link you provided. At least now I am sure that the bean is working quite right. Happy new Year.. – Tahmid Ali Jan 01 '14 at 08:13
  • 2
    Could you change your bean to go along with Java naming standards, i.e., class name `UserList` and fields `userName` and `groupID`, then change the EL to `${iterator.userName}`? Then it should be picked up correctly. – t0mppa Jan 01 '14 at 09:04
  • @t0mppa That was the most silliest java mistake of 2013 done by me. Thanks a lot! That was the exact problem indeed!! – Tahmid Ali Jan 01 '14 at 09:59
  • 1
    So you couldn't pick up the user name because you ignored the naming standards? Thats some interesting behavior. Good to know. You should put this as an answer @t0mppa. – Lama Mar 12 '14 at 12:25

1 Answers1

0

Change the names and use java naming standards like for variable userName, groupID and for class UserList because getter and setter methods for both names are same.
Also change this line
userList user = new userList(rs.getString("USERNAME"), rs.getString("GROUPID")); to this
userList user = new userList( rs.getString("GROUPID"),rs.getString("USERNAME")); due to your constructor.

vivekpansara
  • 895
  • 1
  • 6
  • 14