I want to view the report from web browser, something like :
Copy files from folder jasperreports-5.6.0\demo\samples\webapp\applets :
- commons-collections-3.2.1.jar
- commons-logging-1.1.1.jar
- jasperreports-applet-5.6.0.jar
- JRViewerSimple.class
- EmbeddedViewerApplet.class
Copy to web-app\applets folder (in our grails project)
Create a controller that send an octet-stream, my controller :
import javax.servlet.ServletOutputStream
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
class LaporanController {
def dataSource
def embedbarang() {
String namaFile, reportName, dotJasper
namaFile = "barang"
reportName = grailsApplication.getMainContext().getResource('reports/' + namaFile + '.jrxml').file.getAbsoluteFile()
dotJasper = grailsApplication.getMainContext().getResource('reports/' + namaFile + '.jasper').file.getAbsoluteFile()
// compiles jrxml
JasperCompileManager.compileReportToFile(reportName);
// fills compiled report with parameters and a connection
// JasperPrint print = JasperFillManager.fillReport(reportName + ".jasper", parameters, connection);
JasperPrint print = JasperFillManager.fillReport(dotJasper, null, dataSource.getConnection());
response.setContentType("application/octet-stream");
ServletOutputStream outputStream = response.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outputStream);
oos.writeObject(print);
oos.flush();
oos.close();
outputStream.flush();
outputStream.close();
}
}
Load the applet from .gsp file :
For copy-paste purpose :
<APPLET CODE = "EmbeddedViewerApplet.class" codebase="${request.contextPath}/applets" ARCHIVE = "jasperreports-applet-5.6.0.jar,commons-logging-1.1.1.jar,commons-collections-3.2.1.jar" WIDTH = "100%" HEIGHT = "600"></XMP>
<PARAM NAME = CODE VALUE = "EmbeddedViewerApplet.class" >
<PARAM NAME = CODEBASE VALUE = "${request.contextPath}/applets" >
<PARAM NAME = ARCHIVE VALUE = "jasperreports-applet-5.6.0.jar,commons-logging-1.1.1.jar,commons-collections-2.1.1.jar" >
<PARAM NAME="type" VALUE="application/x-java-applet;version=1.2.2">
<PARAM NAME="scriptable" VALUE="false">
<PARAM NAME = "REPORT_URL" VALUE ="${request.contextPath}/laporan/embedbarang">
</APPLET>
NB: