Using Jasper Reports with Visual Web Pack

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.

  1. /**
  2.  * Output Jasper Report
  3.  *
  4.  * @param filename Precompiled report filename
  5.  * @param type Content type of report ("application/pdf" or "text/html")
  6.  * @param data Collection of value objects
  7.  */
  8. public void jasperReport( String filename, String type, Collection data ) {
  9. jasperReport( filename, type, data, new HashMap() );
  10. }
  11.  
  12. /**
  13.  * Output Jasper Report
  14.  *
  15.  * @param filename Precompiled report filename
  16.  * @param type Type of report ("application/pdf" or "text/html")
  17.  * @param data Collection of value objects
  18.  * @param params Map with parameters
  19.  */
  20. public void jasperReport( String filename, String type, Collection data, Map params ) {
  21. final String[] VALID_TYPES = { "text/html", "application/pdf" };
  22. // First check if type is supported
  23. boolean found = false;
  24. for ( int i = 0; i < VALID_TYPES.length; i++ ) {
  25. if ( VALID_TYPES[i].equals( type ) ) {
  26. found = true;
  27. break;
  28. }
  29. }
  30.  
  31. if ( !found ) {
  32. throw new IllegalArgumentException( "Report type '" + type + "' not supported." );
  33. }
  34.  
  35. // InputStream for compiled report
  36. ExternalContext econtext = getExternalContext();
  37. InputStream stream = econtext.getResourceAsStream( filename );
  38.  
  39. if ( stream == null ) {
  40. throw new IllegalArgumentException( "Report '" + filename + "' could not be opened." );
  41. }
  42.  
  43. // Use collection as data source
  44. JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource( data );
  45. JasperPrint jasperPrint = null;
  46.  
  47. try {
  48. jasperPrint = JasperFillManager.fillReport( stream, params, ds );
  49. } catch ( RuntimeException e ) {
  50. throw e;
  51. } catch ( Exception e ) {
  52. throw new FacesException( e );
  53. } finally {
  54. try {
  55. stream.close();
  56. } catch ( IOException e ) {
  57. }
  58. }
  59.  
  60. // Configure exporter and set parameters
  61. JRExporter exporter = null;
  62. HttpServletResponse response = (HttpServletResponse) econtext.getResponse();
  63. FacesContext fcontext = FacesContext.getCurrentInstance();
  64.  
  65. try {
  66. response.setContentType( type );
  67.  
  68. if ( "application/pdf".equals( type ) ) {
  69. exporter = new JRPdfExporter();
  70. exporter.setParameter( JRExporterParameter.JASPER_PRINT, jasperPrint );
  71. exporter.setParameter( JRExporterParameter.OUTPUT_STREAM,
  72. response.getOutputStream() );
  73. } else if ( "text/html".equals( type ) ) {
  74. exporter = new JRHtmlExporter();
  75. exporter.setParameter( JRExporterParameter.JASPER_PRINT, jasperPrint );
  76. exporter.setParameter( JRExporterParameter.OUTPUT_WRITER, response.getWriter() );
  77. HttpServletRequest request = (HttpServletRequest)
  78. fcontext.getExternalContext().getRequest();
  79. request.getSession().setAttribute(
  80. ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint );
  81. exporter.setParameter( JRHtmlExporterParameter.IMAGES_MAP, new HashMap() );
  82. exporter.setParameter(
  83. JRHtmlExporterParameter.IMAGES_URI,
  84. request.getContextPath() + "/image?image=" );
  85. }
  86. } catch ( RuntimeException e ) {
  87. throw e;
  88. } catch ( Exception e ) {
  89. throw new FacesException( e );
  90. }
  91.  
  92. // Export report
  93. try {
  94. exporter.exportReport();
  95. } catch ( RuntimeException e ) {
  96. throw e;
  97. } catch ( Exception e ) {
  98. throw new FacesException( e );
  99. }
  100.  
  101. // Tell JavaServer faces that no more processing is necessary
  102. fcontext.responseComplete();
  103. }

Start report output from page bean

The output of a report can initiated from a ActionEvent with the following code:

  1. try {
  2. getApplicationBean().jasperReport(
  3. "/reports/report.jasper",
  4. "application/pdf",
  5. getSessionBean().getSuchergebnisDataProvider().getList() );
  6. } catch ( Exception e ) {
  7. Logger.getLogger(getClass().getName()).severe( e.getMessage() );
  8. }
  9.  
  10. return null;

Leave a comment