File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other Open Source Projects and the fly likes How to call Jasper Report Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "How to call Jasper Report" Watch "How to call Jasper Report" New topic
Author

How to call Jasper Report

malik ge
Ranch Hand

Joined: May 13, 2011
Posts: 69
Hello, Is there any way by which I can call JasperReport (.jrxml) file from servlet?
Or is there any other way of doing that?
Any help.
Chandan Sarma
Greenhorn

Joined: Oct 04, 2011
Posts: 14
Hi this is the simple code for jasper report

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author cygnet
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.*;

import java.util.HashMap;

import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperPrintManager;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.export.JRXlsExporter;

public class Report {

public static void main(String args[]) {
JasperReport jasperReport;
JasperPrint jasperPrint;
Connection con = null;
Statement smt = null;

try {
HashMap jasperParameter = new HashMap();

Class.forName("com.mysql.jdbc.Driver");
String URL = "jdbc:mysql://localhost:3306/test";
con = DriverManager.getConnection(URL, "root", "root");
jasperParameter.put("id", new Integer(5));
jasperReport = JasperCompileManager.compileReport("C:/catalog.xml");
jasperPrint = JasperFillManager.fillReport(jasperReport, jasperParameter, con);
JasperPrintManager.printPage(jasperPrint, 0, true);

// JRXlsExporter exporter = new JRXlsExporter();
// exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
// exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "E:/simple_report.xls");
// exporter.exportReport();

} catch (Exception e) {
e.printStackTrace();
}




}
}



and i have store the .xml in C directory withch you can use any folder where you like


<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE jasperReport PUBLIC "-//JasperReports//DTD Report Design//EN"
"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport name="PDFReport" pageWidth="975">
<reportFont name="Arial_Normal" isDefault="true" fontName="Arial" size="15" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
<reportFont name="Arial_Bold" isDefault="false" fontName="Arial" size="15" isBold="true" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica-Bold" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
<reportFont name="Arial_Italic" isDefault="false" fontName="Arial" size="12" isBold="false" isItalic="true" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica-Oblique" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
<parameter name="id" class="java.lang.Integer"/>
<parameter name="ReportTitle" class="java.lang.String"/>

<queryString><![CDATA[SELECT Stu_Id, Stu_Name, Stu_Class FROM stu_table_backup WHERE Stu_Id=$P{id}]]></queryString>
<field name="Stu_Id" class="java.lang.Integer"/>
<field name="Stu_Name" class="java.lang.String"/>
<field name="Stu_Class" class="java.lang.String"/>
<title>
<band height="50">
<textField>
<reportElement x="350" y="0" width="200" height="50" />
<textFieldExpression class="java.lang.String">$P{ReportTitle}</textFieldExpression>
</textField>
</band>
</title>
<pageHeader>
<band>
</band>
</pageHeader>
<columnHeader>
<band height="20">
<staticText>
<reportElement x="0" y="0" width="100" height="20"/>
<textElement>
<font isUnderline="false" reportFont="Arial_Bold"/>
</textElement>
<text><![CDATA[Stu_Id]]></text>
</staticText>
<staticText>
<reportElement x="125" y="0" width="100" height="20"/>
<textElement>
<font isUnderline="false" reportFont="Arial_Bold"/>
</textElement>
<text><![CDATA[Stu_Name]]></text>
</staticText>
<staticText>
<reportElement x="250" y="0" width="150" height="20"/>
<textElement>
<font isUnderline="false" reportFont="Arial_Bold"/>
</textElement>
<text><![CDATA[Stu_Class]]></text>
</staticText>



</band>
</columnHeader>
<detail>
<band height="20">
<textField>
<reportElement x="0" y="0" width="100" height="20"/>


<textFieldExpression class="java.lang.Integer"><![CDATA[$F{Stu_Id}]]></textFieldExpression>
</textField>
<textField pattern="0.00">
<reportElement x="125" y="0" width="100" height="20"/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{Stu_Name}]]></textFieldExpression>
</textField>

<textField pattern="0.00">
<reportElement x="250" y="0" width="150" height="20"/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{Stu_Class}]]></textFieldExpression>
</textField>



</band>
</detail>
<columnFooter>
<band>
</band>
</columnFooter>
<pageFooter>
<band height="15">
<staticText>
<reportElement x="0" y="0" width="40" height="15"/>
<textElement>
<font isUnderline="false" reportFont="Arial_Italic"/>
</textElement>
<text><![CDATA[Page #]]></text>
</staticText>
<textField>
<reportElement x="40" y="0" width="100" height="15"/>
<textElement>
<font isUnderline="false" reportFont="Arial_Italic"/>
</textElement>
<textFieldExpression class="java.lang.Integer"><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
</band>
</pageFooter>
<summary>
<band>
</band>
</summary>
</jasperReport>

you can use this with servlet also but you need to modified the cooding some what
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to call Jasper Report