Ciao,

Vi chiedo numi circa jasper report ed il suo funzionaento, sperando di non essere troppo prolisso e non essere troppo invasivo.

il punto è che non sono solito lavorare con java, e so di fare molta confuzione
Xò mi hanno chiesto di tirare fuori dei report utilizzanto Jasper Report attraverso un'applicazione web (JSP).

Ho provato, sotto il mio ubuntu, a mettermi in moto.
Prima di tutto, preparando l'ambiente: Java2EE 5, Tomcat 6, Jasper Report (JR) ed iReport per ottenere i file jrxml.
Utilizzavo già NetBeans per PHP, ho aggiunto i plugin Java ed agganciato le librerie JR.

il mio obiettivo è tirare fuori qualcosa che vada, utilizzando un db di prova con mysql. Credo di aver ottenuto correttamente il mio jrxml di prova con iReport.

Ora mi chiedo, per ottenere il mio obiettivo (correggetemi se scrivo orrori):
* creo un nuovo progetto Java - web application.
* ci aggiungo una nuova servlet (reportServlet) che dovrà essere richiamata dalla pagina JSP
* modifico la servlet per puntare il mio jrxml
* eseguo il tutto per ottenere un qualcosa che vada


Sfruttando un esempio trovato in rete, ho ottenuto il mio servlet:
codice:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package srcReport;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.OutputStream;

import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.*;
import javax.servlet.*;

import java.sql.*;

import net.sf.jasperreports.view.JasperViewer;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.JRExporterParameter;


/**
 *
 * @author iside
 */
public class reportServlet extends HttpServlet {

    public static String JASPER_REPORT_FOLDER = "./report/template";
    public static String JASPER_FILENAME = "demoReport";

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

    /** Destroys the servlet.
     */
    public void destroy() {
    }

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet reportServlet</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet reportServlet at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
             */

             String JASPER_REPORT_FOLDER   = "/home/iside/NetBeansProjects/WjDemo/report/templates";
             String JASPER_FILENAME    = "/demoReport";
             String DRIVER       = "com.mysql.jdbc.Driver";
             String DB_URL       = "jdbc:mysql://localhost/jrdemo";
             String DB_NAME       = "jrdemo";
             String DB_USERNAME     = "user";
             String DB_PASSWORD     = "pass";

             String jfile = JASPER_REPORT_FOLDER + JASPER_FILENAME + ".jrxml";
             InputStream in = new FileInputStream(jfile);

            JasperPrint jasperPrint =
                    returnReportPrint(DB_NAME, DB_USERNAME, DB_PASSWORD, in);

            PrintWriter jout = response.getWriter();

            JRHtmlExporter exporter = new JRHtmlExporter();
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
            exporter.setParameter(JRExporterParameter.OUTPUT_WRITER,jout);

            exporter.exportReport();

            //exporter.exportReport();

            jout.close();

        } catch(Exception ex) {
           
            out.print(ex.toString());
            
        } finally {

            out.close();
            
        }
    }

    /**
     * Takes 4 parameters: databaseName, userName, password, inputstream
     * and connects to the database and prepares the report.
     * @param databaseName holds database name,
     * @param userName holds user name
     * @param password holds password to connect the database,
     * @param inputstream holds the stream of the Jasper Report file (.jrxml)
     * @return jasperReport holds the jasperReport variable that will be used to export an HTML report
     */
    public JasperPrint returnReportPrint(String databaseName, String userName, String password, InputStream inputStream) {

        JasperPrint jasperPrint = null;
        
        try {
            
            JasperDesign jasperDesign = JRXmlLoader.load(inputStream);
            JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
            Connection jdbcConnection = connectDB(databaseName, userName, password);
            jasperPrint = JasperFillManager.fillReport(jasperReport, null, jdbcConnection);

        } catch (Exception ex) {

            String connectMsg = "Could not create the report stream " + ex.getMessage() + " " + ex.getLocalizedMessage();
            System.out.println(connectMsg);
            
        }
        return jasperPrint;
    }

    /**
     * Takes 3 parameters: databaseName, userName, password
     * and connects to the database.
     * @param databaseName holds database name,
     * @param userName holds user name
     * @param password holds password to connect the database,
     * @return Returns the JDBC connection to the database
     */
    public Connection connectDB(String databaseName, String userName, String password) {
        Connection jdbcConnection = null;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            jdbcConnection = DriverManager.getConnection(databaseName, userName, password);
        } catch (Exception ex) {
            String connectMsg = "Could not connect to the database: " + ex.getMessage() + " " + ex.getLocalizedMessage();
            System.out.println(connectMsg);
        }
        return jdbcConnection;
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    /*
      

    */

}

eseguento reportServlet da index.jsp, il tutto frulla per un poco ma non succede nulla.
Vorrei capire... sono sulla strada giusta? Oppure ho sbagliato tutto, addirittura l'implementazione??

Ogni dritta, consiglio o tips a riguardo è ben accetto.
Ringrazio davvero tutti per la pazienza e l'aiuto che vorrete darmi!!!!!