This project I'm working I'm trying to get the servlet to redirect me to a page from a search input where there are all the users with the same name or surname, basically a search query.
Problem is it all works but if I add the last condition, where it should send me to a "User not found" page, then it won't find my users anymore, only the first entry in my database.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String utente = request.getParameter("user");
listaUtenti = accountDAO.selectAllAccounts(); //recupero tutti gli utenti del database e li metto in una lista
System.out.println("Stampo lista utenti" + listaUtenti.toString());
List<Account> utentiTrovati = new ArrayList<Account>(); //creo nuova lista per gestire gli utenti che trovo
RequestDispatcher rd;
if(!(listaUtenti.isEmpty())) {
//per ogni account nella lista
for (Account acc : listaUtenti) {
String nomecognome = acc.getName() + " " + acc.getSurname();
Account account = new Account(acc.getName(), acc.getSurname(), acc.getAge(), acc.getCountry());
//se il parametro che ho inserito è uguale ad accountnome + accountcognome
if (utente.equalsIgnoreCase(nomecognome)) {
utentiTrovati = accountDAO.selectAllAccountsNameSurname(acc.getName(), acc.getSurname());
System.out.println("Utenti trovati" + utentiTrovati.toString());
request.setAttribute("utenti_trovati", utentiTrovati);
rd = request.getRequestDispatcher("paginautenti.jsp");
rd.forward(request, response);
return;
} else if (utente.equalsIgnoreCase(acc.getName())) {
utentiTrovati = accountDAO.selectAllAccountsName(acc.getName());
System.out.println("Utenti trovati" + utentiTrovati.toString());
request.setAttribute("utenti_trovati", utentiTrovati);
rd = request.getRequestDispatcher("paginautenti.jsp");
rd.forward(request, response);
return;
} else if (utente.equalsIgnoreCase(acc.getSurname())) {
utentiTrovati = accountDAO.selectAllAccountsSurname(acc.getSurname());
System.out.println("Utenti trovati" + utentiTrovati.toString());
request.setAttribute("utenti_trovati", utentiTrovati);
rd = request.getRequestDispatcher("paginautenti.jsp");
rd.forward(request, response);
return;
} else if (!listaUtenti.contains(account)) {
rd = request.getRequestDispatcher("usernotfound.jsp");
rd.forward(request, response);
return;
}
}
} else {
rd = request.getRequestDispatcher("Errore.html");
rd.forward(request, response);
}
}