This tutorial illustrates the use of Jasper Reports with a Visual Web Pack application.
Register Jasper Reports library
Use the NetBeans Library Manager to create a library for the Jasper Reports class libraries. You need at least the following files from the distribution:
- dist/jasperreports-<version>.jar
- lib/commons-beanutils-1.7.jar
- lib/commons-collections-2.1.jar
- lib/commons-digester-1.7.jar
- lib/commons-logging-1.0.2.jar
- lib/itext-1.3.1.jar
Register Jasper Reports image servlet
The image servlet is needed if you want html rendered reports (also without any graphical elements, because report placeholders uses images from this servlet). So you must register it in the web.xml configuration file. You can use the NetBeans web.xml editor to do so.
Servlet name : ImageServlet Servlet class : net.sf.jasperreports.j2ee.servlets.ImageServlet URL : /image
Insert methods for report output to application bean
The following methods in the application bean can be used to output a precompiled report as html or pdf. In this sample a collection of java objects is used as data source. For other data sources see the Jasper Reports documentation.
/**
* Output Jasper Report
*
* @param filename Precompiled report filename
* @param type Content type of report ("application/pdf" or "text/html")
* @param data Collection of value objects
*/
public void jasperReport( String filename, String type, Collection data ) {
jasperReport( filename, type, data, new HashMap() );
}
/**
* Output Jasper Report
*
* @param filename Precompiled report filename
* @param type Type of report ("application/pdf" or "text/html")
* @param data Collection of value objects
* @param params Map with parameters
*/
public void jasperReport( String filename, String type, Collection data, Map params ) {
final String[] VALID_TYPES = { "text/html", "application/pdf" };
// First check if type is supported
boolean found = false;
for ( int i = 0; i < VALID_TYPES.length; i++ ) {
if ( VALID_TYPES[i].equals( type ) ) {
found = true;
break;
}
}
if ( !found ) {
throw new IllegalArgumentException( "Report type '" + type + "' not supported." );
}
// InputStream for compiled report
ExternalContext econtext = getExternalContext();
InputStream stream = econtext.getResourceAsStream( filename );
if ( stream == null ) {
throw new IllegalArgumentException( "Report '" + filename + "' could not be opened." );
}
// Use collection as data source
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource( data );
JasperPrint jasperPrint = null;
try {
jasperPrint = JasperFillManager.fillReport( stream, params, ds );
} catch ( RuntimeException e ) {
throw e;
} catch ( Exception e ) {
throw new FacesException( e );
} finally {
try {
stream.close();
} catch ( IOException e ) {
}
}
// Configure exporter and set parameters
JRExporter exporter = null;
HttpServletResponse response = (HttpServletResponse) econtext.getResponse();
FacesContext fcontext = FacesContext.getCurrentInstance();
try {
response.setContentType( type );
if ( "application/pdf".equals( type ) ) {
exporter = new JRPdfExporter();
exporter.setParameter( JRExporterParameter.JASPER_PRINT, jasperPrint );
exporter.setParameter( JRExporterParameter.OUTPUT_STREAM,
response.getOutputStream() );
} else if ( "text/html".equals( type ) ) {
exporter = new JRHtmlExporter();
exporter.setParameter( JRExporterParameter.JASPER_PRINT, jasperPrint );
exporter.setParameter( JRExporterParameter.OUTPUT_WRITER, response.getWriter() );
HttpServletRequest request = (HttpServletRequest)
fcontext.getExternalContext().getRequest();
request.getSession().setAttribute(
ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint );
exporter.setParameter( JRHtmlExporterParameter.IMAGES_MAP, new HashMap() );
exporter.setParameter(
JRHtmlExporterParameter.IMAGES_URI,
request.getContextPath() + "/image?image=" );
}
} catch ( RuntimeException e ) {
throw e;
} catch ( Exception e ) {
throw new FacesException( e );
}
// Export report
try {
exporter.exportReport();
} catch ( RuntimeException e ) {
throw e;
} catch ( Exception e ) {
throw new FacesException( e );
}
// Tell JavaServer faces that no more processing is necessary
fcontext.responseComplete();
}
Start report output from page bean
The output of a report can initiated from a ActionEvent with the following code:
try {
getApplicationBean().jasperReport(
"/reports/report.jasper",
"application/pdf",
getSessionBean().getSuchergebnisDataProvider().getList() );
} catch ( Exception e ) {
Logger.getLogger(getClass().getName()).severe( e.getMessage() );
}
return null;