bom dia,
Não estou conseguindo carregar a listar e acusa erro se “AssertionError” se comento o Assertion() no fim do Switch case a página carrega, mas a lista não.
BD no MySQL:
BD bd_ventas
Columns:
IdEmpleado | int UN PK |
---|---|
Dni | varchar(8) |
Nombres | varchar(255) |
Telefono | varchar(9) |
Estado | varchar(1) |
User | varchar(8) |
Servlet
package controlador;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
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 dao.EmpleadoDAO;
import modelo.Empleado;
@WebServlet("/Controlador")
public class Controlador extends HttpServlet {
Empleado em = new Empleado();
EmpleadoDAO edao = new EmpleadoDAO();
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String menu = request.getParameter("menu");
String action = request.getParameter("action");
if(menu.equals("principal")) {
request.getRequestDispatcher("principal.jsp").forward(request, response);
}
if(menu.equals("empregado")) {
switch ("action") {
case "listar":
List lista = edao.listar();
//System.out.println(lista);
request.setAttribute("empleados", lista);
break;
case "agregar":
break;
case "editar":
break;
case "eliminar":
break;
default:
throw new AssertionError();
}
request.getRequestDispatcher("empregado.jsp").forward(request, response);
}
if(menu.equals("produto")) {
request.getRequestDispatcher("produto.jsp").forward(request, response);
}
if(menu.equals("clientes")) {
request.getRequestDispatcher("clientes.jsp").forward(request, response);
}
if(menu.equals("registrarvenda")) {
request.getRequestDispatcher("registrarvenda.jsp").forward(request, response);
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
dao , só o listar
public class EmpleadoDAO {
.
.
.
public List listar() { //15:49
String sql = "SELECT * FROM empleado ";
List lista = new ArrayList<>();
try {
PreparedStatement pst = conexaoBD.prepareStatement(sql);
ResultSet rs = pst.executeQuery();
while(rs.next()) {
Empleado em = new Empleado();
em.setId(rs.getInt(1));
em.setDni(rs.getString(2));
em.setNom(rs.getString(3));
em.setTel(rs.getString(4));
em.setEstado(rs.getString(5));
em.setUser(rs.getString(6));
lista.add(em);
//System.out.println(lista);
}
} catch (Exception e) {
// TODO: handle exception
}
return lista;
}
que no teste do métodp main, funcionou:
public static void main(String[] args) {
EmpleadoDAO empdao = new EmpleadoDAO();
try {
List<Empleado> emps = empdao.listar();
for (Empleado e : emps) {
System.out.println("name: " + e.getNom() + " user: "+e.getUser());
}
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
name: Pedro Hernandez user: emp01
name: Roman Riquelme user: Jo46
name: Palermo Suarez user: Em22
name: marcel motta user: marcel
Aqui a parte do jsp que manda parâmetros, no caso " action" e “menu”
> <li class="nav-item"><a
> style="margin: left 10px; border: none"
> class="btn btn-outline-light" href="Controlador?menu=empregado&action=listar" target="myFrame" >Employees</a></li>
A tabela com for:Each que deveria carregar na pág empregados.jsp
${em.getId()} ${em.getDni()} ${em.getNom()} ${em.getTel()} ${em.getEstado()} ${em.getUser()} editar delete </tr> </c:forEach> </tbody> </table>
aqui os erros no console:
fev 22, 2021 11:12:49 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [controlador.Controlador] in context with path [/SistemasVentasWeb] threw exception [Servlet execution threw an exception] with root cause
java.lang.AssertionError
at controlador.Controlador.processRequest(Controlador.java:51)
at controlador.Controlador.doGet(Controlador.java:114)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)HTTP Status 500 – Internal Server Error
Type Exception Report
Message Servlet execution threw an exception
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
javax.servlet.ServletException: Servlet execution threw an exception org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
aqui o erro: clicar no botão acima