Once more!!can anyone help me ?

cold you help me with these codes, my jsp page brings
me no result, a blank page. Can you tell where's the mistake?
Thanks in advance!!
this is my context:
package teste;
import java.util.ArrayList;
import javax.servlet.*;
public final class ContextPage implements ServletContextListener {
    private ServletContext context = null;
    Data bookDB;
    public void contextInitialized(ServletContextEvent event) {
        context = event.getServletContext();
        try {           
            bookDB = new Data();          
            context.setAttribute("bookDB", bookDB);
        } catch (Exception ex) {
            System.out.println("Couldn't create bookstore database bean: " +
                ex.getMessage());
    public void contextDestroyed(ServletContextEvent event) {
         context = event.getServletContext();
        Data dados = (Data) context.getAttribute("bookDB");
             if (dados != null) {
                 dados.remove();
        context.removeAttribute("bookDB");              
}   this is the class to store the data:
package teste;
public class Detalhes {
    private String titulo;
    private String autor;
    private Double preco;
    private int codigo;
    public Detalhes(String titulo, String autor, Double preco, int codigo) {
        this.titulo = titulo;
        this.autor = autor;
        this.preco = preco;
        this.codigo = codigo;       
    public String getTitulo(){
        return titulo;
    public String getAutor(){
        return autor;
    public Double getPreco(){
        return preco;
    public int getCodigo(){
        return codigo;
    public void setTitulo(String titulo){
        this.titulo = titulo;
    public void setAutor(String autor){
        this.autor = autor;
    public void setPreco(double preco){
        this.preco = preco;
    public void setCodigo(int codigo){
        this.codigo = codigo;
}this is my conection and where i call the object puting the data
package teste;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Data {
    PreparedStatement prepStmt;
    ResultSet rs;
    Connection con;
    Detalhes detalhes;
    int codigo;
    ArrayList list;
    public Data()throws Exception {       
        try {
            Class.forName("org.gjt.mm.mysql.Driver");
            con = DriverManager.getConnection("jdbc:mysql://127.0.0.1/loja?user=marcos&password=131283");            
        } catch (Exception ex) {
            throw new Exception(" Database not found!!" +
                ex.getMessage());
    public void remove() {
        try {
            con.close();
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
    public void setCodigo(int codigo){
        this.codigo = codigo;
    public Detalhes getDetalhes(){               
        try {
            String selectStatement = "select * from livraria where codigo ?";           
            prepStmt = con.prepareStatement(selectStatement);
            prepStmt.setInt(1, codigo);
            rs = prepStmt.executeQuery();
            while(rs.next())
                 detalhes = new Detalhes(rs.getString("titulo"), rs.getString("autor"), rs.getDouble("preco"), rs.getInt("codigo"));          
            prepStmt.close();
        }catch(Exception e){           
            System.out.println(e.getMessage());
        }finally {
           // close prepared statements and result sets within finally blocks
           if (rs != null) {
              try {
                 rs.close();
              } catch (SQLException sqle) {
                  System.out.println(sqle.getMessage());
           if (prepStmt != null) {
              try {
                   prepStmt.close();
              } catch (SQLException sqle) {
                  System.out.println(sqle.getMessage());
        return detalhes;
    public ArrayList getList(){
        try {           
            list = new ArrayList();
            String selectStatement = "select * from livraria";
            PreparedStatement prepStmt = con.prepareStatement(selectStatement);
            ResultSet rs = prepStmt.executeQuery();
            while(rs.next()){
                 detalhes = new Detalhes(rs.getString(1), rs.getString(2), rs.getDouble(3), rs.getInt(4));          
                 list.add(detalhes);
            prepStmt.close();                       
        }catch(Exception e){
            System.out.println(e.getMessage());
        return list;
}and at last my jsp page
<p><b><h1>Resultado:</h1></b></p><br>
<jsp:useBean id="sourceData" class="teste.Data" scope="application" />
<c:set var="lista" value="${sourceData}">
<c:forEach var="book" begin="0" items="${lista}">
    <p><b><h1>${book.titulo}</h1></b></p><br>
</c:forEach>why does it brings me a blank page?Thanks for replying!!

what are you printing out on the page?List of book titles :)
Soneca,
Your jsp is iterating over an ampty list, that's why you get a blank page. Data has to be set in scope, before you can use it in your jsp page. Your context listener sure has some stuff, but it sets an empty BookDB object - you have a lot of rework to do on your code.
cheers,
ram.

Similar Messages

Maybe you are looking for