I am following this POJO's example to output a list of users with the Object id
but show nothing in the list. I'm not sure if the connection is correct with the CodecRegistry
or something wrong in one of my below classes. Tried to use the Eclipse debugger but got no hints with that.
Firstmost the connection
ConnectionString connectiontring = new ConnectionString(
"my connection to database");
MongoClientSettings clientSettings = MongoClientSettings.builder().codecRegistry(pojoCodecRegistry)
.applyToConnectionPoolSettings(builder -> builder.maxWaitTime(20000, TimeUnit.MILLISECONDS))
.applyConnectionString(connectiontring).retryWrites(false).build();
MongoClient mongoClient = MongoClients.create(clientSettings);
MongoDatabase database = mongoClient.getDatabase("DATABASE");
Here is my list user method in DAO class
@Override
public List<User> listAll() {
List<User> userList = new ArrayList<User>();
// 1. get the table from dB
MongoCollection<Document> userTbl = database.getCollection("User");
// loop through the table to find, then add
for (Document doc : userTbl.find()) {
User user = new User((String) doc.get("fullname"), (String) doc.get("email"), (String) doc.get("password"));
userList.add(user);
}
return userList;
}
Service class just call the method from DAO class
public List<User> listUser() {
List<User> userList = userDAO.listAll();
return userList;
}
The Controller
public class ListUsersServlet extends HttpServlet {
public ListUsersServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
UserService userService = new UserService(request, response);
List<User> userList = userService.listUser();
String list_user_page = "user_list.jsp";
RequestDispatcher rd = request.getRequestDispatcher(list_user_page);
rd.forward(request, response);
}
}