0

I have a problem with my lista-dados.jsp: My 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);
        }

    }

}

My Dao:

 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;
    }
}

And i have a webform to look a specific data:

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

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

i'm able to login in my home.jsp via servlet. and i have some doubt with my lista-dados.jsp whats the problem when i put the cpf number and press the submit button i get the error:

    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)
Wesley Heron
  • 413
  • 2
  • 12
  • 27
  • i can not see any variable with name of cpf in your `ConsultaDados` class. where is it? is in the super class? – Rasoul Taheri Jul 18 '13 at 20:08
  • @RasoulTaheri But in my class Contato that i dont put in this thread there is a variable called cpf with your getters/setters methods. and my class ConsultaDados call a instance of this public Contato getContato(String cpf ) – Wesley Heron Jul 18 '13 at 20:37
  • when you use `${dao.cpf}` this means you have a variable with name cpf that is public or hava getter method. and it placed in the current class or its super class. – Rasoul Taheri Jul 18 '13 at 20:46
  • 1
    when you use ${dao.cpf} it will call `getCpf()` method in the your dao object. while your dao object (ConsultaDados class) has not any method with this name. – Rasoul Taheri Jul 18 '13 at 20:56
  • @RasoulTaheri you have a sugestion how may i implement this ?webservices is the best option or servlet? – Wesley Heron Jul 18 '13 at 21:05
  • @Wesley what part of **your dao object (ConsultaDados class) has not any method with this name** is hard to understand? Why are you talking about web services? – Luiggi Mendoza Jul 18 '13 at 21:19
  • @Wesley webservice and servlet are two different thing. about this question dont confuse your think with servlet and webserver. just create a method in the your ConsultaDados with name `getCpf()` and in the this method handle any opertation that you will. then return a String value. – Rasoul Taheri Jul 19 '13 at 09:12

1 Answers1

1

using ${dao.cpf} means that there should be an instance variable called cpf and a getter method for it as getCpf(){return this.cpf} in your class.

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
  • This should be a comment rather than an answer since it doesn't provide a solution for the problem. Also, following [How to avoid Java Code in JSP-Files?](http://stackoverflow.com/q/3177733/1065197), you should use EL and JSTL or ``, not both at the same time, but it recommends use JSTL over ``. – Luiggi Mendoza Jul 18 '13 at 20:24
  • @LuiggiMendoza i dont understand i use <%@ page import="br.com.cad.servlet.PesquisaCPF,br.com.cad.dao.ConsultaDados" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> in my lista-dados.jsp – Wesley Heron Jul 18 '13 at 20:39
  • @LuiggiMendoza How may i add piece of code in my comments in this thread? – Wesley Heron Jul 18 '13 at 20:41