Guide for Jasper Reports Drill Down facility inside a web application

Recently one of our delivery team wanted us to demonstrate how drill down feature in Jasper Report can be used for their project.  I was going through Jasper web site and various blogs to provide the team with a easy steps to create drill down reports, but I could not find all the information in one place. I have to use contents from various blog to make it work. So I thought it would be worth to put this experience of creating drill down report on this blog.

These 11 easy steps will enable you to create drill down jasper reports using iReport tool:

  1. Open IReport, go to File –> New, in the displayed dialog box please select “Blank A4” and click on “Open this Template” button.
  2. Now in subsequent dialog box, provide the report name and directory location where the file need to be created.
  3. In the displayed Designer window, drag the “Chart” from Palette window and place it in “Title” section.
  4. Now select the Chart in the Designer and go to the Properties window to make the following changes:
    • Set “Background Alpha” to “0.5
    • Set “Foreground Alpha” to “0.5
    • Set “Depth Factor” to “0.2”
  5. Now click on the “Report Query” in the toolbar. In the displayed dialog box, specify the following query
  6. “select shipcountry, count(*) as order_count from orders group by shipcountry”.

    (Note: Here we are using HSQL sample db supplied with jasperreports-3.6.2 installation as our database for this   demo).

  7. Now right click on Chart in the Designer and click on Chart Data. It will open Chart Details dialog box. In the Details tab, specify the values as given in the below figure.
  8. In the Section Hyperlink sub-tab, specify following details:
    • Hyperlink target = “Self
    • Hyperlink type = “Reference”
    • Reference tab, Hyperlink Reference Expression, specify ““DetailedReport.jsp?shipcountry=” + $F{SHIPCOUNTRY}
    • Tooltip tab, specify “$F{SHIPCOUNTRY}”
  9. These settings will provide the drill down hyperlink facility to each slice inside the Pie chart and passes the selected country name as filter to the drill down report.

    Note: Instead of using Hyperlink typeReference”, it is ideal to use “ReportExecution”, but in my case it is not working (I think it is due to some bug in iReport 3.6.2 community edition release).

  10. Now click on the Preview to test the report.
  11. For drill down feature to work we need to create another report which takes country name as an input filter and displays orders of the given country. This is straight forward report and guidelines for the same can be found in the jasper quick start guide.
  12. In our case we have provided following input as Report Query for detailed report.

    “select * as order_count from orders where shipcountry=$P{shipcountry}”

    Below is the sample detailed report that is created for our demo.

  13. To view this inside a web application we need to insert following code in our JSP/Servlet files.
    • <<DrillDownMainChart.jsp>>: Below given code generates the HTML contents with drill down hyperlinks for each slice in the chart. Upon clicking the hyperlink, it will take us to detailed page.
    • File reportFile = new File(application.getRealPath(“/reports/DrillDownMainChart.jasper”));
      if (!reportFile.exists()) throw new JRRuntimeException(“File DrillDownMainChart.jasper not found. The report design must be compiled first.”);

      JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath());

      Map parameters = new HashMap();
      JasperPrint jasperPrint =
      JasperFillManager.fillReport(jasperReport, parameters, WebappDataSource.getConnection());

      JRHtmlExporter exporter = new JRHtmlExporter();
      session.setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
      exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
      exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
      exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, “../servlets/image?image=”);
      exporter.exportReport();

    • <<DetailedReport.jsp>>: Below given code generates detailed report based on the criteria supplied criteria (in our case it “shipping country”) from the previous page.
    • File reportFile = new File(application.getRealPath(“/reports/DrillDownDetailed.jasper”));
      if (!reportFile.exists()) throw new JRRuntimeException(“File DrillDownDetailed.jasper not found. The report design must be compiled first.”);

      JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath());

      Map parameters = new HashMap();
      parameters.put(“shipcountry”, request.getParameter(“shipcountry”));

      parameters.put(JRParameter.IS_IGNORE_PAGINATION, Boolean.TRUE);

      JasperPrint jasperPrint =
      JasperFillManager.fillReport(jasperReport, parameters, WebappDataSource.getConnection());

      JRHtmlExporter exporter = new JRHtmlExporter();

      session.setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
      exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
      exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
      exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, “../servlets/image?image=”);

      exporter.exportReport();

  14. Sample output screens (from our demo application)
  15. <<First Screen with drill down links to each slice (in this case lets click on Germany)>>

    <<Second Screen with detailed report based on the filter (shipping country) selected in previous screen (in this case it was Germany)>>

Author: Madhusudhan

Lead Architect at Capgemini India

39 thoughts on “Guide for Jasper Reports Drill Down facility inside a web application”

  1. Dear Madhu,

    It was a very wounderful post.It helped me a lot. Can you please tell me the steps for creating HTML drill down reports. I need it urgently.
    Thanks,

    1. Hi Pradeep,

      HTML drill down is straight forward.
      Please look at the sample application “demo\samples\hyperlink” under your jasper reports installable to understand how to build HTML drill down feature.

      Hope this will guide you.

      1. Hi Madhu,

        I tried the steps you have explaind in blog, i followed same steps i am able to run JSP code sucsessfully but its showing me blank screen, its look like its not able to get that chart.
        Can you please let me know what is the reason.

        Looking forward for your reply

        Thanks and Regards
        Aezaz Ahmed

      2. Hi Madhu,

        i have posted my issue to you in previous post. Its related to that in your jsp code below line is there

        exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, “../servlets/image?image=”);

        …/servlets/image?image=”);
        what this means. wether we need to build image servlet pls clear my issue. when i run my code i am able to get only alignments not chart image.

        looking for you reply

        Regards
        Aezaz

  2. Hi Madhu,

    Nice article!! Can drill down reports be implemented for standalone (Swing) based applications?

    Any Suggestions?

    Cheers,
    Shanks

    1. Hi Shanks,

      Never tried out Jasper Reports in Swing based applications. If you can tell me exact problem/exception that you are facing, I can spend some time and can get back to you with a possible solution.

      regards
      Madhu

    1. hey can u share the full jsp code ..cs i cannot understand exactly what to put in jsp file..

      regards Deepankar Singh

  3. Hi,

    Thanks for your article. I want to show the drill down report in same page. ie I want to expand/collapse the subreport on clicking +/- symbol. Please help me to achieve this using iReports and Jasper.

    + MCA
    + MBA
    – BE
    Sl.No Name Mark
    1 Prasad 89
    2 Babu 78
    + ME

    Thanks & Regards,
    Prasad Babu. J

  4. Nice article… I could create Pie charts in IReport toll and same in JSP’s.
    I am able to see all the rows as pie charts in output. But I want only final output as pie chart..I mean as output like main report in your article,include all the countries in final output.

    1. It’s continues to my previous post..
      I want to see final output(include all rows in database) as in one pie chart.But once I excuted each row shows as pie chart till final output.If I want to see final output now I have to go last page for look the final output.Same problem in IReport and JSP’s once I excute. Is there any changes required in JRXML while we design IReport?

  5. can smboody plzzz….share the jsp code….cos i cnt undersand which packages to import in order to run the jasper files
    waiting for ur reply

    Regards Deepankar Singh

  6. Following are the imports used in our jsp:

    import=”datasource.*”
    import=”net.sf.jasperreports.engine.*”
    import=”net.sf.jasperreports.engine.util.*”
    import=”net.sf.jasperreports.engine.export.*”
    import=”net.sf.jasperreports.j2ee.servlets.*”
    import=”java.util.*”
    import=”java.io.*”

    import=”java.awt.image.ImageObserver”
    import=”java.awt.image.BufferedImage”
    import=”javax.imageio.ImageIO”
    import=”java.awt.Graphics2D”
    import=”com.sun.image.codec.jpeg.JPEGImageEncoder”
    import=”com.sun.image.codec.jpeg.JPEGCodec”

    hope this will help….

    1. Sir,
      I have included all the packages but still i m getting error at the following lines..

      session.setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
      exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);

      at session in first line
      and at out in second line….
      …w8ing for ur reply..

      Regards
      Deepankar Singh

  7. Pingback: proxxon
  8. Hi Madhu garu,
    Thats a very good article which helped me a lot.

    I have used some flex charts in my reports. But I am unable to view the charts in the Jasper Server after adding the input controls(If there are no input controls then the chart is visible). The report is working fine, we can say this because on exporting the report to PDF in Jasper Server the chart is visible but not in the viewer.

    Please can you help me in resolving the issue..

    Thank You,
    Sunil.

  9. hi,
    i am using this for creating report bt i am not able to understand how to created jsps for this code

  10. Hi Madhu,

    I want to create a drilldown report in ireports 3.0.0.
    can you please let me know which steps need to follow.

    Thanks,
    Harsha Reddy

  11. there is some problem with hyperlink reference expression……….. it dose not work on ireport 4.60

  12. Hi, I am new to Jaspersoft. I nned to create drill down report . My report is like this. master report: It display country wise total numbers & in detail report i have city wise total numbers. I need to create a drill down report like if i click any country thatshould give that countries city wise report. If some one help me it will be great

  13. Good day sir. Can you make another example using the latest CE Edition of Jasper Report And Ireport and making a step by step hyperlink drilldown reporting using a main report with a Candlestick chart?

    Thank you Sir.

  14. I blog likewise and I am posting a thing alike to this particular blog post,
    “Guide for Jasper Reports Drill Down facility inside a web application Madhusudhan Reddy Blog”.
    Do you care if I reallyutilise some of your concepts?
    I appreciate it ,Mayra

  15. Hi,

    I’m getting below error in DetailedReport.jsp

    An error occurred at line: 29 in the jsp file: /DetailedReport.jsp
    WebappDataSource cannot be resolved
    26: parameters.put(JRParameter.IS_IGNORE_PAGINATION, Boolean.TRUE);
    27:
    28: JasperPrint jasperPrint =
    29: JasperFillManager.fillReport(jasperReport,parameters, WebappDataSource.getConnection());
    30:
    31: JRHtmlExporter exporter = new JRHtmlExporter();
    32:

    Stacktrace:
    at org.

  16. hey there and thank you for your information – I’ve definitely picked up anything new from right here. I did however expertise several technical issues using this site, since I experienced to reload the site a lot of times previous to I could get it to load properly. I had been wondering if your hosting is OK? Not that I’m complaining, but
    slow loading instances times will sometimes affect
    your placement in google and can damage your high-quality score if ads and marketing with
    Adwords. Anyway I’m adding this RSS to my email and could look out for much more of your respective fascinating content. Ensure that you update this again very soon.

  17. What if, if iam using report on jasper server, i dont have the servlets and Jsps…/
    I mean the URL in expression.

  18. Hi Madhu,
    I done all the configuration but i am getting empty screen with some layouts.
    Can you please tell where i have done mistake?

  19. hi nukalamadhu, can you help me.
    I need to create slice and dice report, but i not using any chart, can you give me a sample to make it with normal table ?

  20. How to drill within a report like bo,cognos we can drill up down by on a set of columns which are created as hierarchy so when a column s clicked it drills down to next column within a report can we achieve this?if so how?
    secondly how to link two report blocks within a report like table and chart whe ni click on chart it should filter the table within a report .

  21. in drill down reports as it is above getting fine. but second report -detailed report not loading. am not getting any errors also. what might be the problem? if i pass values to second report i can able to see report preview in jasper studio

Leave a comment