1
Good evening, I am unable to display a list that is saved in the database on the jsp page. I would like to know how I do, have tried some ways, call for menagerBean and Servlet
jsp calling on Servlet
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="entity.*, persistence.*"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<jsp:useBean id="mb" class="manager.ManagerBean" scope="request"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Times</title>
</head>
<body>
<form id="vt" action="Controle?cmd=listar" method="get">
<table border=1 >
<tr>
<th> Codigo </th>
<th> Nome </th>
<th> Email</th>
<th> Sexo</th>
<th> Nascimento</th>
<th> Time</th>
</tr>
<c:forEach var="linha" items="${listar}" >
<tr>
<td> ${linha.idTorcedor}</td>
<td> ${linha.nome}</td>
<td> ${linha.email}</td>
<td> ${linha.sexo}</td>
<td> ${linha.dataNascimento}</td>
<td> ${linha.time}</td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
jsp calling for the bean manager
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="entity.*, persistence.*"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<jsp:useBean id="mb" class="manager.ManagerBean" scope="request"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Times</title>
</head>
<body>
<form id="vt" action="Controle?cmd=listar" method="get">
<table border=1 >
<tr>
<th> Codigo </th>
<th> Nome </th>
<th> Email</th>
<th> Sexo</th>
<th> Nascimento</th>
<th> Time</th>
</tr>
<c:forEach var="linha" items="${listar}" >
<tr>
<td> ${linha.idTorcedor}</td>
<td> ${linha.nome}</td>
<td> ${linha.email}</td>
<td> ${linha.sexo}</td>
<td> ${linha.dataNascimento}</td>
<td> ${linha.time}</td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
Managerbean
package manager;
import java.util.List;
import entity.Torcedor;
import persistence.TorcedorDao;
public class ManagerBean {
private List<Torcedor>torcedores;
public ManagerBean() {
// TODO Auto-generated constructor stub
}
public List<Torcedor> getTorcedores() {
try{
TorcedorDao td = new TorcedorDao();
torcedores = td.findAll();
}catch(Exception e){
}
return torcedores;
}
public void setTorcedores(List<Torcedor> torcedores) {
this.torcedores = torcedores;
}
}
Servlet
package control;
import java.io.IOException;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import entity.Torcedor;
import persistence.TorcedorDao;
/**
* Servlet implementation class Controle
*/
@WebServlet("/Controle")
public class Controle extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String cmd = request.getParameter("cmd");
if(cmd.equalsIgnoreCase("listar")){
listar(response,request);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String cmd = request.getParameter("cmd");
if(cmd.equalsIgnoreCase("login")){
login(response,request);
}
if(cmd.equalsIgnoreCase("gravar"));
gravar(request,response);
}
protected void gravar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String msg= "";
try {
String nome = request.getParameter("nome");
String email = request.getParameter("email");
String sexo = request.getParameter("sexo");
String dataNascimento= request.getParameter("dataNascimento");
String time= request.getParameter("time");
Torcedor t= new Torcedor(null,nome,email,sexo,dataNascimento,time);
new TorcedorDao().gravar(t);
msg="Dados Gravados com Sucesso";
} catch (Exception e) {
msg = "Erro" + e.getMessage();
e.printStackTrace();
}finally{
request.setAttribute("msg", msg);
request.getRequestDispatcher("visualizarTime.jsp").forward(request,response);
}
}
protected void login (HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
String email = request.getParameter("email");
Torcedor torcedor = null;
try{
torcedor= new TorcedorDao().findByEmail(email);
request.setAttribute("torcedor", torcedor);
if(email==null){
request.getRequestDispatcher("visualizarTime.jsp").forward(request, response);
request.setAttribute("listar", new TorcedorDao().findAll());
}else{
request.getRequestDispatcher("cadastrarTorcedor.jsp").forward(request, response);;
}
}catch (Exception e) {
e.printStackTrace();
}
}
protected void listar (HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
try{
request.setAttribute("listar", new TorcedorDao().findAll());
request.getRequestDispatcher("visualizarTime.jsp").forward(request,response);
}catch(Exception e){
e.printStackTrace();
}finally {
}
}
}
Somebody help me
– Mattos