After successfully make a template report (*.jrxml & *.jasper) using iReport tools, the next step is to ‘call’ the *.jasper file so it can be printed by a web application page using Java (JSP). These are the steps that we need to do (according to the author experiences).
- Prepare these libraries, usually it’s already available on ‘lib’ directory in iReport installation folder.
- commons-beanutils-1.7.jar
- commons-collections-2.1.jar
- commons-digester-1.7.jar
- iReport.jar
- itext-1.3.1.jar
- jasperreports-3.0.0.jar
- jstl.jar
- mysql-connector-java-3.1.11-bin.jar
- poi-3.0.1-FINAL-20070705.jar
- standard.jar
- xalan.jar
Then, we have to copy those files to ‘lib’ directory on ‘WEB-INF’ in a web project. Below the screenshoot of Eclipse IDE.
- Now, we can create a simple JSP file, as example, we name it as ‘employeereport.jsp’ :
- Import package/class
<%@ page import="java.io.*, java.util.*, net.sf.jasperreports.engine.*, net.sf.jasperreports.engine.export.*" %>
- Define the taglib
<%@ page pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c-rt" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt-rt" %> - Type these code/scriptlet
<%
Map parameters = null;
JRExporter jrExporter = null;
//locate the file & its path *.jasper in this example is ‘BasicFeatures.jasper’
JasperPrint jasperPrint =
JasperFillManager.fillReport(
application.getRealPath("/hrdapp/report/jasper/BasicFeatures.jasper"), parameters, new JREmptyDataSource());OutputStream outputStream = response.getOutputStream();
response.setContentType("application/pdf");
jrExporter = new JRPdfExporter();
jrExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
jrExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream);
jrExporter.exportReport();
outputStream.close();
%>
- Import package/class
- And now, we can access on localhost and we can get a PDF file output
Download Docs File (Bahasa Indonesia)