Re: Jasper report execution in java with parametrized query at run time
Abhijit Kamatkar <abhijitkamatkar@...> writes:
> I have been working on Jasper Reports. I have created few reports using
> ireport and I was able to export these reports within java. Of course these
> reports were simple. Now I want to get deeper in Jasper reports. I want to
> know how one can execute jasper report in java with parametrized query at
> run time. Has anyone worked on similar module?
If you've already gotten the basics of running the report from within
a piece of Java code then you're most of the way there.
All you need to do to add parameter support to your existing code is
to supply any necessary parameters (as a java.util.Map) to
JasperFillManager.fillReport, and those parameters will be available
to the report.
How you go about obtaining those parameters is very dependent on the
environment in which your Java code is running. If it's a command
line application you might take parameters from the command line,
whereas a web server might take it from the URL or form data. You
could opt to have some way of referring to an input file containing
the parameters, whether in XML or some other format. But in any case,
and via any mechanism you prefer, you're responsible for obtaining the
parameters and turning them into the Map that you give to
As an example, to support my own use of JasperReports (a back-end PDF
report generator for a web application), I implemented a small web
application - that I run beneath Jetty - that listened on an internal
loopback port. My main application isn't in Java, so I needed some
clean way for it to use the report server to generate reports when
I initial experimented with a command line utility but the web
application avoids the overhead of loading the Java runtime and
establish the database connections for each report request. As it is
the Jetty-based report server has a footprint several times larger
than my entire main application, though I'm not complaining since it
does the reporting job well.
My main web application internally generates web requests (essentially
does its own GETs from localhost) to the report server, and the report
server's GET handler then:
1. Uses the last segment of the URL to identify the report name.
2. Identifies the jrxml and jasper files for that report (from a
configured filesystem root location)
3. If the jrxml is later than the jasper, recompiles the report
4. Takes any query parameters from the URL and turns them into a
HashMap. For simplicity all parameters are passed on to
fillReport as a string.
5. Fills the report (including the parameter map)
6. Streams the result back to the requester as a PDF using
JasperExportManager.exportReportToPdfStream. The main application
typically just passes this stream right back to the client.
During initial experimentation, I simplified things a little by
skipping step 3 and just directly loading jasper files built in
iReport. This works as long as your iReport and JasperReports library
versions are reasonably in sync.
Note also that, in general, step 4 could be an opening to a SQL
injection attack depending on how parameters are supplied and used.
In my case, the only client to the reporting server is my own web
application which has either generated the parameters internally, or
already sanitized/validated any parameters from the client browser.
I found it easier to implement all parameters as strings since that
simplified the URL needed to the reporting server. But if I had
needed accurate type information I could have encoded that in the URL
and then instantiated appropriately typed objects to supply as
Download Intel® Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.