this is my DB enquiry function
public static List<UserPass> selectAllUser() {
ConnectionPool pool = ConnectionPool.getInstance();
Connection connection = pool.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
List<UserPass> usersPass = new ArrayList<UserPass>();
String query = "select * from UserPass";
try{
ps = connection.prepareStatement(query);
rs = ps.executeQuery();
while(rs.next()){
UserPass users = new UserPass();
users.setUserName(rs.getString("user_name"));
usersPass.add(users);
}
return usersPass;
and this is my UserPass object get function
public ArrayList<UserPass> getusernames(){
return usernames;
}
my servlet dopost is simply this
request.setAttribute("users", UserPassDb.selectAllUser());
String forward = "/testpage.jsp";
RequestDispatcher view = request.getRequestDispatcher(forward);
view.forward(request, response);
and my display jsp page is
<form action="TestServlet" method="post">
<input type="submit">
</form>
<c:forEach var="user" items="${users}">
<c:out value="${user.usernames}"/>
</c:forEach>
UserPass Class
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package business;
import java.io.Serializable;
import java.util.ArrayList;
/**
*
* @author One
*/
public class UserPass implements Serializable {
public String username;
public String password;
public ArrayList<UserPass> usernames;
public UserPass(){
this.username ="";
this.password ="";
this.usernames = new ArrayList<UserPass>();
}
public void setUserName(String username) {
this.username = username;
}
public String getUserName(){
return username;
}
public ArrayList<UserPass> getusernames(){
return usernames;
}
public void setusernames(ArrayList<UserPass> a){
this.usernames = a;
}
}
tried changing this it tells me property doesn’t exist when it does as far as i understand. am sure am wrong
Thanks in advance
You have
getusernames()which returnsArrayListand You did not mentionedsetusernames(ArrayList<UserPass> ).But, If You want to display Usernames, In JSP, Change
Here
usersis the request attribute which is aList<UserPass>. Now,When you iterate it usingforEach,You getUserPassinuserfor each row.Then
user.usernamecallsgetUserNamefromUserPassbean.In your case
user.usernames, It returns emptyArraylist.