JasperReports Tutorial: Dynamic Drill-Down Reports with MongoDB (Part 2 of 2)

1 Comment

Let’s continue our work begun in part one of the tutorial!

Step 3: The drilldown

Step 3.1: iReport to JasperServer

First, let’s connect iReport to the JasperServer, so that working with it becomes easier.

In iReport, click on the Repository Navigator (if it’s not available select “Window” -> “JasperReports Server Repository”) followed by the “Add new server”-button:

Screen 38

In the dialogue that opens, we’ll need to provide the data required by iReport to successfully connect to the server:

  • ID: an arbitrary name to identify the connection. This will be used in the Repository Navigator.
  • URL: the URL to reach the REST-API interface of the server. For you, the defaults should be fine. I had to install the server on another computer because my evaluation license expired while writing this tutorial, so I could no longer use localhost 😉
  • Organization: enter ‘organization_1’.
  • Username/Password: enter ‘jasperadmin’ in both fields.

Screen 39

Click “Save” and then check if the connection works by expanding the newly created server-node in the Repository Navigator:

Screen 40

In case you are wondering, which other users are available on the server you can open the login page in a browser (http://<host>:<port>/jasperserver-pro/login.html) and click on the following link:

Screen 41

This will give an overview of the users available by default / in the trial edition.

Step 3.2: The datasource (again?)

Yes, again: reports don’t connect to a datasource per se; it’s the execution environment that does so – up until now that has been iReport (for the report previews).

Since we want to execute the report in the server to use drill downs, the server needs to get access to our MongoDB-instance.

In the Repository Navigator, right-click on the “Data Sources” node , select “Add” followed by “Data Source”. This will open the following dialogue:

Screen 42

You’ll have to provide an ID and a name. The ID should not contain whitespaces or special characters because it is used in the URI for the REST-API and if you’d wanted to access it, you’d have to URL-encode those characters.

The name is used in the Repository Navigator, so can contain any character you like.

The second tab called “Data Source Details” contains the actual connection information. Just enter the same as you did when configuring the datasource for iReport:

Screen 43

Click “Save” to store the datasource.

Now, let’s test if it works.

Step 3.3: The report on the server

Now it’s time to export our report to the server and check if everything works.

First, in the Repository Navigator, right-click on the “Reports”-node, select “Add” -> “Folder” give it the ID and name “Omnibus” and click “Save”.

Now, right-click the newly created folder, select “Add” and choose “JasperServer Report”. This opens a wizard that requires you to provide ID and name on its first page. Enter “Main” for both and click “Next”.

The next page asks you to provide the actual report:

Screen 44

Select “Locally Defined” and provide the path to the report; either by clicking “Browse” or if you have the report open by clicking “Get source from current opened report”.

Hit “Next” to continue and provide the datasource to use:

Screen 44_2

Select “From the repository”, click “Browse” and locate the datasource we created previously.

Click “Finish” to store the report on the server:

Screen 45

Open a browser and login to the server with user “jasperadmin”.

In the menu, choose “View” and select “repository” to open the server repository:

Screen 46

Locate the report’s node we just created in iReport and execute the report by following the link named “Main”:

Screen 47

After a short while you should be presented with your familiar report.

I believe, congratulations are in order 🙂

Step 3.4: The drilled down report

Now, let’s create the view of the report that will show the filtered results.

This is done best by copying the main report and then modifying that copy accordingly.

So, to get started, repeat step 3.1 but name the new report “Detail” instead of “Main”:

Screen 48

Make sure, that it’s working by executing it on the server.

Back in iReport, close any open report files, locate the report we just created in the server repository and open it by double-clicking it. This should open a temporary file named “file_<really long number>.jrxml”:

Screen 49

Whenever you edit a report from and want to update the version on the server, you can right-click on the report-file in the Repository Navigator and select “Replace with current document”.

Now, instead of walking you in all detail through the steps required to change the report, I will let you try to get it working – it isn’t that difficult. The screenshots at the start of this tutorial and the following hints should let you do it:

  • The report will need the following parameters:
    • question: the question that was selected (that is: drilled-down from).
    • answer: the answer that was selected (that is: drilled-down from).
    • respondent_ids: a comma separated list of those respondent_ids, that chose the selected answer to the selected question.
  • The query needs to be changed to take the respondent_ids into account – we want to filter the data, after all (you can access JasperReport parameters in a query with ‘$P’; add a ‘!’ to let JasperReport handle quoting as appropriate):
    • findQuery: {respondent_id: {‘$in’: [$P!{respondent_ids}]}}

Ok, ok, I won’t be cruel – you can download the finished detail report from here:


You also need to provide JasperReports Server input controls for the three parameters that are required by the report. These input controls are used to present users of the reports with a gui to input the values themselves as well as to automatically map HTTP request parameters to the corresponding values; the latter feature is the one that we will be using.

In the Repository Navigator, locate the node for the detail report; expand it and right-click on the node “Input Controls” and choose “Create a local input control”. In the dialogue that opens you have to provide the following data:

  • “General” tab
    • ID: this has to be the parameter name as used in the report. In our case “question”, “answer” and “respondent_ids” respectively.
    • name: an arbitrary name that will be displayed to the user; I used “Question”, “Answer” and “Respondent IDs”.
  • “Input Control Details” tab
    • Type: select “Single Value” for each.
    • Mandatory: true
    • Visible: false (if you choose true, the user will be presented a dialogue to input values even when they are automatically provided by request parameters.)
    • Locate (Datatype): Select “From Repository”, click “Browse” and navigate to “datatypes”.
      Screen 50
      Select “TextGeneralDatatype” from that folder and click “Open Resource”.
      Screen 51

Hit “Save”.

Make sure to create input controls for all three parameters we need. After you have done so you can even test the detail report; JasperReports Server will now show you a more meaningful error message than before:

Screen 52

Step 3.5: Link main and detail

We are almost done (really!)

All that is left to do is linking main and detail so that the user can click on a pie of a chart and filter the report by the selected answer.

In order to do that, we need our main report to provide the parameters required by the detail report – while enhancing its pie chart to function as a hyperlink.

Collecting the respondent_ids

Before the respondent_ids can be passed to the detail report, they need to be collected in a variable.

In the Repository Navigator, locate the main report and open it by double-click. Then in the Report Inspector right-click on “Variables” and select “Add Variable”. The following properties need to be configured for the new variable:

  • Name: arbitrary name; I chose “respondent_ids”.
  • Reset type: configures, when the variable should be reset (cleared). Since we want the “respondent_ids” to be collected for each answer-group, we select “Group”.
  • Reset group: select the group to reset on; “answer”, of course.
  • Variable Expression: enter the expression to create a comma separated list of the respondent_ids (remember: it’s Groovy):
    • ($V{respondent_ids}==””?$F{respondent_id}:$V{respondent_ids}+”,”+$F{respondent_id})
  • Initial Value Expression: the empty String “”.

Screen 53

You can check if the variable is correctly generated by placing a “Text Field” into the “answer Group Footer 1” band, selecting “$V{respondent_ids}” for its expression (you might also want to enable “Stretch With Overflow” so that all values get printed) and generating a preview – be warned, though, that the preview will take a while to generate and will be around 200 pages long.

Save your work and upload it to the server.

Hyperlinking from chart to detail

We now have all the ingredients available to hyperlink from the pie chart of the main report to the detail report.

In general, when one wants to hyperlink to a report being executed on the JasperReports Server, one can drag the target report (that is, the one being linked to) unto the component on the originating report (the one being linked from) and let iReport generate the necessary parameters.

However, since we want a different hyperlink from each section of the pie chart, we cannot use this mechanism as is. We can only use the link-parameter “_report” that iReport generates for us:

Screen 54

In the Repository Navigator, drag the Folder (not the report file!) of our detail report unto the pie chart element of the main report.

This causes a dialogue to open, which you could use to configure a hyperlink – but that we cannot use (as stated above). However, open the tab “Link parameter” and note the value of the parameter “_report” – we are going to need that shortly:

Screen 55

The value is the path of IDs (not names!) to locate the detail report using the REST-API.

We do not want this hyperlink, so for “Hyperlink target” select “Blank” and for “Hyperlink type” select “None” before closing the dialogue.

Next, right-click the pie chart and select “Chart Data”. In the following dialogue, select the tab “Details”, “Pie series” and “Section hyperlink” and configure the values I did:

Screen 56

The values should by self-explanatory by now.

Save your work and upload it to the server.

Let’s try it out!

When you execute the main report, the pie charts should now by hyperlinks. But, if you click one, you should get to see something like this:

Screen 57

What is it NOW? (It’s not working, dude!)

Take a look at the URL that the browser tried to load. Here is mine:

In other words: it’s too long!

Most, if not all HTTP servers have a limit of the amount of bytes they are accepting in an HTTP header – Apache Tomcat, which JasperReports Server uses in our standalone version, is no exception and accepts 8192 bytes by default. Anything bigger will get truncated and therefore will most likely not constitute a valid HTTP header.

Since it is not possible to somehow get JasperReports Server to use HTTP-POST instead of HTTP-GET to transmit the request parameters, we need to (and indeed, we can) configure Tomcat to increase this limit using the maxHttpHeaderSize parameter; 65Kb should be enough.

Locate the installation directory of your JasperReport Server installation. Inside, locate the directory “apache-tomcat/conf” and open the file “server.xml”.

Look for the connector for port 8080 (the default; if you installed JasperReports Server on another port you’ll have to locate that connector instead) and add the parameter:

Screen 58

Save and close the file and restart JasperReports Server.

When you check the reports again, the hyperlinks from main to detail should be working.

Finally, we are done – really. No more errors; it should truly work as desired.

That’s it from me. I hope, you’ve found my tutorial worthwhile and if you want to dig deeper into the ins and outs of JasperReports, I suggest reading the various ultimate guides available on the following JasperSoft site:

Thank’s for your time!


  • Rudra

    I am not able to get the drilled down link when I am converting this to HTML output using Java API.
    Please suggest if any workaround is there.


Your email address will not be published. Required fields are marked *