'Listing data with jsp:useBean

I have some doubts about a problem with my lista-dados.jsp.

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.ArrayList" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>


<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="utf-8" />
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="dao" class="bean.ListaDados" scope="session"/>
<table>

<c:forEach var="contato" items="${dao.lista}">
<tr>
<td>${contato.nome}</td>
<td>${contato.email}</td>
<td>${contato.endereco}</td>
<td>${contato.dataNascimento.time}</td>
</tr>
</c:forEach>
</table>
</body>
</html>

My Class ListaDados:(WEBContent/WEB-INF/classes/bean)

     package bean;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import br.com.cad.basica.Contato;
import br.com.cad.dao.ConnectDb;
public class ListaDados extends ConnectDb {
    Connection c = this.getConnection();
    PreparedStatement ps = null;
    ResultSet rs = null;
    public List<Contato> getContatos(String cpf) {
         try {
         List<Contato> contatos = new ArrayList<Contato>();
         ps = c.prepareStatement("select * from dados_cadastro where pf_cpf= ? ");
         ps.setString(1, cpf);
         ResultSet rs = ps.executeQuery();

         if (rs.next()){

         Contato contato = new Contato();
         contato.setEmail(rs.getString("pf_email"));
         contato.setNome( rs.getString("pf_nome") );
         contato.setEmail(rs.getString("pf_email"));
         contato.setEndereco(rs.getString("pf_endereco"));


         Calendar data = Calendar.getInstance();
         data.setTime(rs.getDate("pf_dt_nasc"));
         contato.setDataNascimento(data);


         contatos.add(contato);
         }
         rs.close();
         ps.close();
         return contatos;

         } catch (SQLException e) {
        throw new RuntimeException(e);
     }

   }
}

This is my checa.jsp:

<%@ page language="java" import="java.sql.Connection,java.sql.DriverManager,java.sql.SQLException" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="utf-8" />
<title>Painel Administrativo</title>
<c:choose>
    <c:when test="${ user eq null }">
        <jsp:forward page="erroLogin.jsp" />
    </c:when>
    <c:otherwise>
        <p><h2>Bem-vindo, ${ user.nome }</p></h2>

</head>
<body>
<form action="lista-dados.jsp" method="POST"> 
<label for="cpf">Busque o CPF</label>
<br>
<input type="text" id="cpf" required name="cpf">
<button type="submit">Consultar</button>
<%
            String msg=(String) request.getAttribute("msg");
            if(msg!=null)
                out.println(msg);
%>
</c:otherwise>

</c:choose>  
</form>
</body>
</html>

When I enter a doc number in my servlet checa.jsp and press a submit button, I get an error:

HTTP Status 500 - /lista-dados.jsp (line: 14, column: 0) The value for the useBean class attribute bean.ListaDados is invalid.
    org.apache.jasper.JasperException: /lista-dados.jsp (line: 14, column: 0) The value for the useBean class attribute bean.ListaDados is invalid.
    org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:42)
    org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:443)
    org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:149)
    org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1237)
    org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1182)
    org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2376)
    org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2428)
    org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2434)
    org.apache.jasper.compiler.Node$Root.accept(Node.java:475)
    org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2376)
    org.apache.jasper.compiler.Generator.generate(Generator.java:3503)
    org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:250)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:373)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

I don't understand this error, what's the problem?


The error continues i think my bean there is error but i dont know whats!!

  package bean;
    import br.com.cad.basica.Contato;
import br.com.cad.dao.ConnectDb;
public class ListaDados extends ConnectDb {
    public List<Contato> getContatos() {
         try {
         List<Contato> contatos = new ArrayList<Contato>();
         PreparedStatement stmt = this.getConnection()
         .prepareStatement("select * from dados_cadastro where pf_cpf= ?");
         ResultSet rs = stmt.executeQuery();

         while (rs.next()) {
contatos.add(contato);
         }
         rs.close();
         stmt.close();
         return contatos;

         } catch (SQLException e) {
        throw new RuntimeException(e);
     }

   }
}

         Contato contato = new Contato();

         contato.setNome(rs.getString("pf_nome"));

There is a jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
        <jsp:useBean id="bean" class="bean.ListaDados" />
        <jsp:setProperty name="bean" property="*"/>
        <table>

        <c:forEach var="contato" items="${bean.contatos}">
        <tr>
        <td>${contato.nome}</td>
        </c:forEach>
    </table>

I think the error is just with my ListaDados class but i dont know is correct?



Solution 1:[1]

You must correct your class according to the following rules:

Following are the unique characteristics that distinguish a JavaBean from other Java classes:

  1. It provides a default, no-argument constructor.

  2. It should be serializable and implement the Serializable interface.

  3. It may have a number of properties which can be read or written.

  4. It may have a number of "getter" and "setter" methods for the properties.

    public class ListaDados extends ConnectDb implements java.io.Serializable{ public ListaDados(){ } //Your Code haere }

Solution 2:[2]

you only can use the tag :

<jsp:useBean id="dao" class="bean.ListaDados" scope="session"/>

for Objects Bean -> a object Bean is a object that have one construct empty ant gets and sets for ech one of the attributes od the class and implements serializable

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 procrastinator