简体   繁体   中英

Best Way to generate Jasper Report using Java/JSP/Servlet

Scenario - User input(Date Range, Report Type) is taken from JSP page & passed to servlet then to java.

I want to ask whether i should -

  1. Use mysql query in java & then put values in jasper using Hashmap? or
  2. Use mysql query in jasper itself & just provide data connection using java?

Main concern is which approach is better in terms of performance, robustness, future changes?

Well your question is so general. What is your approach and your business model? If your query has so much complexity , It seems that it is better to use sql directly in jasper and it will be faster than using values in jasper to generate report . But in programming paradigm , there is just a one module to know about database connection , and that is your domain side of program . I'd prefer do not initialize your database connection in your jasper configuration so just use values in your jasper and map them with Hashmap. You can use from your previous connection and database module (module that uses for your entities or domain objects ) in your program and fetch the desire data and map them to your report

Use mysql query in java & then put values in jasper using Hashmap?

This sounds crazy, a pure Hashmap is not a datasource so you will not be able to iterate on it.

Your solutions can be:

  1. Use mysql query in java & then put values in JRDatasource, pass datasource to jasper report

  2. Use mysql query in jasper itself & just provide data connection using java

  3. Generate mysql query in java and pass query and data connection to jasper report

  1. The only scenario that I would use this is when data can not be extracted in a simple way from a single query, all data is not available in query or data is already available in memory (es. users has selected rows in a table).

  2. When developing reports this is the "normal" and probably the quickest one, tools like iReport, gives you the possibility to extract the fields from query automatically and preview the report with live data connection (you do not need to compile and run your code). This way the developer of the report is also stand-alone , very useful in large projects.

  3. When query gets where complex (depends on multiple variables), the query can be passed directly as parameter (or multiple parameters). In the report a default expression for the query can be used to have the advantage of 2.

So normally you use metod 2.

performance : if data is already in memory 1 is quicker (no sql) otherwise you will probably only consum more memory (hence when you load a JRDataSource). However I have not done any extensive testing.

Robustness , depends more on your code then method used

future changes , 2 has the advantage of changing only in one place "the report", you do not even need to open the java project.

If my understanding is correct, application has user selected filters for reports. These filter conditions are passed to the app in HTTP Servlet Request and you want to choose between

  1. providing the query in < queryString > and field mapping in jrxml design file, with only required where condition parameters passed from java
  2. implementing the report content retrieval within java and pass the java beans to the jrmxl file

I would suggest to align with the project's overall back-end design strategy for fetching data from DB.

If your reports are relatively simple and application already has a fully fledged ORM implementation use the second approach. This will be relatively simple in initial implementation as well as implementation of any future change requests.

If this is not the case and sql queries for generating reports are dynamic and fairly complex I would still suggest to proceed with option 2.

  • Write the queries(string builder) for report data retrieval in java or maintain them as functions/procedures in DB
  • populate a collection of java beans and pass it to jasper report engine

Either approach may not have any significant performance difference as long as application is running on a app server with decent specs. If performance is critical and current sever resources as limited I would suggest to - run reports as a separate app in a separate JVM - separate report and app db servers and use DB replication tools to sync data across them. Generating huge complex reports tends to slow down the app performance very much.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM