Search code examples
javamongodbservletsmongo-java

how to check exist user and pass in mongoCollection for set in session?


what can I write in if() to check the parameters taken from the login form exist in the collection or not?

String username = request.getParameter("user");
String password = request.getParameter("pass");
String[] remeberusr = request.getParameterValues("remember");

MongoClient client = new MongoClient("localhost", 27017);
DB db = client.getDB("mydb");
DBCollection collection = db.getCollection("members");
BasicDBObject setquery = new BasicDBObject();
setquery.put("user", username);
setquery.put("pass", password);

DBCursor result = collection.find(setquery);

if (?)) {
    HttpSession session = request.getSession();
    session.setAttribute("user", username);
    session.setAttribute("pass", password);
    session.setAttribute("remember", remeberuser);
    response.sendRedirect("session.jsp");
} else {
    out.print("user and pass incorrected!");
}

Solution

  • if(hasNext()) 
    

    hasNext() Checks if there is another object available