report and subreport jasper

I wrote this servlet for exporting in excel.
All works perfect when I have a simple report.
But when I add a subreport to my report the output is an empty browser....
I put the subreport jasper in the SUBREPORT_PATH specified.
What I'm doing wrong?

package servlet;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.Map;

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

import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.export.JExcelApiExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
import net.sf.jasperreports.engine.xml.JRXmlLoader;

public class ExportStats extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;

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

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Connection con = null;
final JRExporter exporter = new JExcelApiExporter();

ServletOutputStream servletOutputStream = response.getOutputStream();
try {
Class.forName("com.mysql.jdbc.Driver");
final String url = "jdbc:mysql://localhost:3306/mara";
con = DriverManager.getConnection(url, "mera", "aram");
final String repName = request.getParameter("RN");
final URL fileUrl = this.getClass().getResource(repName);

JasperPrint jasperPrint = null;

final JasperDesign design = JRXmlLoader.load(fileUrl.openStream());
final JasperReport report = JasperCompileManager.compileReport(design);
jasperPrint = JasperFillManager.fillReport(report, getModel(request), con);

if (jasperPrint.getPages().size()>0) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
exporter.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, baos);
exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,Boolean.TRUE);
exporter.setParameter(JRXlsExporterParameter.MAXIMUM_ROWS_PER_SHEET,Integer.decode("65000"));
exporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
exporter.exportReport();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "filename="+repName.substring(0,repName.length()-6)+".xls");
System.out.println(repName.substring(0,repName.length()-6)+".xls");
servletOutputStream.write(baos.toByteArray());
servletOutputStream.flush();
servletOutputStream.close();
} else {
response.setHeader("content-type", "text/html");
try {
servletOutputStream.println("");
servletOutputStream.println(" ");
servletOutputStream.println(" Export in excel");
servletOutputStream.println(" ");
servletOutputStream.println(" ");
servletOutputStream.println("

");
servletOutputStream.println("

The report contains no data. Check filter parameters.

");
servletOutputStream.println("

");
servletOutputStream.println(" ");
servletOutputStream.println("

");
servletOutputStream.println(" ");
servletOutputStream.println("");
System.out.println("no rows..... 5");
} catch (Exception e) {}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (servletOutputStream != null) {
servletOutputStream.close();
}
} catch (Exception e) {
} try {

} catch(Exception e) {
}try {
if (con != null) {
con.close();
}
} catch(Exception e) {
}
}
}
private static String createWhereCondition(HttpServletRequest request) {
final String couCod = request.getParameter("CO");
final String fromYear = request.getParameter("FY");
final String toYear = request.getParameter("TY");
final Integer fromAge = Integer.parseInt(request.getParameter("L").equals("undefined") ? "0" : request.getParameter("L"));
final Integer toAge = Integer.parseInt(request.getParameter("U").equals("undefined") ? "100" : request.getParameter("U"));
final StringBuffer where = new StringBuffer();

if (!couCod.equals("undefined")) {
where.append(" AND mar_country.cou_cod =").append(couCod);
}
final String repName = request.getParameter("RN");
if (repName.equals("locations.jrxml")) {
if (!fromYear.equals("undefined") && !toYear.equals("undefined") ){
where.append(" AgeGroupLower >= ").append(fromAge).append(" AND AgeGroupUpper<=").append(toAge).append(" and loc_cod is not null)");
}
} else {
if (!fromYear.equals("1900") || !toYear.equals("2009")){
where.append(" and DateStartYear >= ").append(fromYear).append(" AND DateCompleteYear<=").append(toYear).append(" And ");
}

}

System.out.println(where.toString());
if (where.toString().length()==0) {
return "";
} else {
return where.toString();

}
}

private Map getModel(HttpServletRequest request) {
final String QueryPart = createWhereCondition(request);
final Map parameters = new HashMap();
parameters.put("desQueryPart", QueryPart);
final String repName = request.getParameter("RN");
final String path = this.getClass().getResource(repName).toString();
parameters.put("SUBREPORT_DIR",path.substring(0, path.length() - (repName.length() +1)) + File.separatorChar);
System.out.println(request.getSession().getServletContext().getRealPath("/WEB-INF/classes/servlet"));
System.out.println(" SUBREPORT_DIR: "+path.substring(0, path.length() - (repName.length() +1)) + File.separatorChar);
return parameters;
}
}

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mavirroco's picture

maybe

when use subreports

1)put SUBREPORT_DIR var in master report

2)inherit the parameter SUBREPORT_DIR in subreport from master report

..Hope this Help

better if post your output from log file....

Manuel Rodriguez Coria
Tarija, Bolivia

www.mavirroco.com/contact.html
mrodriguez@mavirroco.com

not working.... the error is

not working....
the error is coming during the FillReport phase...
Maybe because I've to pass somehow the subreport to the main report? Or I don't know... Something missing. No error in the log. I Attach the 2 report (rep and subreport) maybe is helpful...

AttachmentSize
report.zip 2.97 KB
mavirroco's picture

my updated report

Did you fixed??

Hi,

I am having the very same problem. I can generate a report wihout problems if I don't include any subreport.
Mu java service is the one i provide bellow, the problem arises when FillManager method is reached.

The path for all the subreports is the absolut path where the main report is located and the rest of subreports too.
I have no problems generating the report via iReport (usign the preview tool).

Any suggestion? Should I load the rest of subreports too?

try {
      Class.forName(driver).newInstance();
      conn = DriverManager.getConnection(url+dbName,userName,password);
      System.out.println("Connected to the database");
     
     
     
      Map parameters = new HashMap();
      ArrayList<Integer> idList=new ArrayList<Integer>();
     
      for (int i=0;i<ids.length;i++){
        idList.add(ids[i]);
      }
        
      parameters.put("id_indicators",idList);
       
      URL fileUrl = this.getClass().getResource("report4.jrxml");
      JasperDesign jasperDesign = JRXmlLoader.load(fileUrl.openStream());
      System.out.println("After Load.");
      JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
      System.out.println("After Jasper Compile Manager.");
      //Database jdbcConnection = new Database();
      JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,parameters, conn); //
      System.out.println("After FileManager");
      JasperExportManager.exportReportToPdfFile(jasperPrint, "C:/Users/Documents/WaveMaker 6.4.4GA/projects/myproject/webapproot/resources/reports/report.pdf");

      System.out.println("End");
   
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
    try {
if (conn != null) {
conn.close();
}
} catch(Exception e) {
}
    }

GOTTA?