public List<Cadastro> getLista() {
try {
List<Cadastro> cadastros = new ArrayList<Cadastro>();
PreparedStatement stmt = this.connection.prepareStatement("select * from cadastro");
ResultSet rs = stmt.executeQuery();
while (rs.next()){
Cadastro cadastro = new Cadastro();
cadastro.setNome(rs.getString("nome"));
cadastro.setIdade(rs.getInt("idade"));
cadastro.setTelefone(rs.getString("telefone"));
cadastro.setEmail(rs.getString("email"));
cadastros.add(cadastro);
}
rs.close();
stmt.close();
return cadastros;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
Tenho uma servlet (ConsultarCadastroServlet.java) que preciso que chame o método listado acima, recupere a lista contendo os resultados e envie para uma jsp (consultarcadastro.jsp).
Só tenho isso na servlet até agora:protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
CadastroDAO dao = new CadastroDAO();
List<Cadastro> cadastros = dao.getLista();
} finally {
out.close();
}
}
Como faço pra essa servlet recuperar os resultados e enviar para a jsp ?
Obrigado!