0

我的 lista-dados.jsp 有问题: 我的 Servlet:

 package br.com.cad.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.com.cad.basica.Contato;
import br.com.cad.dao.ConsultaDados;

public class PesquisaCPF extends HttpServlet{
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
                         throws ServletException, IOException{

        HttpSession session = request.getSession(); 
        Contato user = null;

        String cpf = request.getParameter("cpf"); 
        System.out.println(cpf);
        try {
            ConsultaDados dao = new ConsultaDados(); 
            user = dao.getContato(cpf);
        }
        catch ( Exception e ){
             System.out.println(e.getMessage()); 
        }


        if ( user == null ) {
            session.invalidate();
            request.setAttribute("msg", "CPF não encontrado");
            request.getRequestDispatcher("checa.jsp" ).forward(request, response);

        }
        else{

            session.setAttribute("user", user);
            request.getRequestDispatcher("checa.jsp" ).forward(request, response);
        }

    }

}

我的道:

 package br.com.cad.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import br.com.cad.basica.Contato;
public class ConsultaDados extends ConnectDb{

public Contato getContato(String cpf ){

        Connection c = this.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{


            ps = c.prepareStatement("select * from dados_cadastro where pf_cpf = ?");

            ps.setString(1, cpf);

            rs = ps.executeQuery();

            if ( rs.next() ){
                Contato user = new Contato();
                user.setEmail(rs.getString("pf_email"));
                user.setSenha(rs.getString("pf_senha"));
                user.setNome( rs.getString("pf_nome") );
                user.setSobrenome( rs.getString("pf_ultimonome") );
                user.setCpf( rs.getString("pf_cpf") );
                user.setRg( rs.getString("pf_rg") );
                //user.setDataNascimento("pf_dt_nasc");
                user.setTelefone( rs.getString("pf_telefone") );
                user.setCelular( rs.getString("pf_celular") );
                user.setSexo( rs.getString("pf_sexo") );
                user.setEndereco( rs.getString("pf_endereco") );
                user.setBairro( rs.getString("pf_bairro") );
                user.setCidade( rs.getString("pf_cidade") );
                user.setEstado( rs.getString("pf_estado") );
                user.setSangue( rs.getString("pf_sangue") );
                user.setAlergia( rs.getString("pf_alergia") );
                user.setCirurgia( rs.getString("pf_cirugia") );
                user.setDiabete( rs.getString("pf_diabete") );
                user.setSoropositivo( rs.getString("pf_soro_positivo") );
                return user;
            }
        }
        catch (SQLException e){
            e.printStackTrace();
        }
        finally{
            if (rs != null ) {
                try { rs.close(); } catch (SQLException e) { ; }
                rs = null;
            }
            if (ps != null ) {
                try { ps.close(); } catch (SQLException e) { ; }
                ps = null;
            }
            if (c != null ) {
                try { c.close(); } catch (SQLException e) { ; }
                c = null;
            }
        }
        return null;
    }
}

我有一个网络表单来查看特定数据:

    <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><...>

我的 lista-dados.jsp:

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="br.com.cad.servlet.PesquisaCPF,br.com.cad.dao.ConsultaDados" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="utf-8" />
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="dao" class="br.com.cad.dao.ConsultaDados"/>

<table>

${dao.cpf}
<tr>
<td>${user.nome}</td>
<td>${user.email}</td>
<td>${user.endereco}</td>

</tr>

</table>


</body>
</html>

我可以通过 servlet 登录我的 home.jsp。我对我的 lista-dados.jsp 有一些疑问,当我输入 cpf 编号并按下提交按钮时,我得到了错误:

    exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /lista-dados.jsp at line 17

14: 
15: <table>
16: 
17: ${dao.cpf}
18: <tr>
19: <td>${user.nome}</td>
20: <td>${user.email}</td>


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
    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)
root cause

javax.el.PropertyNotFoundException: Property 'cpf' not found on type br.com.cad.dao.ConsultaDados
    javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:237)
    javax.el.BeanELResolver$BeanProperties.access$400(BeanELResolver.java:214)
    javax.el.BeanELResolver.property(BeanELResolver.java:325)
    javax.el.BeanELResolver.getValue(BeanELResolver.java:85)
    org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:104)
    org.apache.el.parser.AstValue.getValue(AstValue.java:183)
    org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
    org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:1026)
    org.apache.jsp.lista_002ddados_jsp._jspService(lista_002ddados_jsp.java:85)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    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)
4

1 回答 1

1

using${dao.cpf}意味着应该有一个被调用的实例变量cpf和一个 getter 方法,就像getCpf(){return this.cpf}在你的类中一样。

于 2013-07-18T20:06:50.397 回答