Dynamic Jasper with Example

Dynamic Jasper Report  – Making dynamic reports easy

DynamicJasper (DJ) is an open source free library that hides the complexity of Jasper Reports, it helps developers to save time when designing simple/medium complexity reports generating the layout of the report elements automatically. DJ creates reports dynamically, defining at runtime the columns, column width (auto width), groups, variables, fonts, charts, crosstabs, sub reports (that can also be dynamic), page size and everything else that you can define at design time.

DJ keeps full compatibility with Jasper Reports since it’s a tool that helps creating reports in a programmatic friendly way, taking care of the report design for you.

Classic .jrxml files can be used as templates while the content and layout of the report elements are handled by the DJ API.


100% pure Java

  • No need to use other tool than you favorite IDE.
  • Friendly and intuitive API.
  • Mature, robust and stable.

Dynamic column report: Columns can be defined at runtime, which means you also control (at runtime) the column positioning, width, title, etc.

Repeating groups / Breaking groups: Create repeating groups dynamically using simple expressions as criteria or complex custom expressions. Each repeating group may have a header and/or footer, which can have a variable showing the result of an operation (SUM, COUNT or any other provided by Jasper Reports).

Automatic report layout: Just define a minimum set of options and DJ will take care of the layout. It’s not an issue to generate the same report for different page sizes and orientation many more!

Dynamic Crosstabs: Jasper Report’s popular crosstabs can now be created dynamically in an easy and convenient way.

Sub reports

  • Sub reports are supported; they can also be dynamically created.
  • Concatenating many reports in a single one (e.g.: a single PDF) can be a hard task. Using DynamicJasper it is really easy get reports of different nature in a single one.

Styles: Each column can have its own style for its title and detail data (defining border, border color, font size, type and color, background color, etc.).

Style library from jrxml files are supported.

Calculation Variables: Repeating groups can have variables that hold the result of an operation on a given field (column). With DJ adding variables is a 1 line of code task.

JRXML template files support: You can use a base template jrxml file in which common styles, company logo, water mark, etc can be pre defined.

Conditional Format: DJ provides a very simple way to define conditional formats. You can use simple conditions or custom conditions.

Auto text: Add auto text in page header and footer such as ?Page 1 of 10?, ?Generated on Oct. 10th 2007? or a custom text.

Charts: Easy to add simple charts.

Barcode columns: As simple as adding a regular column.

Export to most popular formats: As DJ stands over Jasper Reports, it can export to PDF, XML, HTML, CSV, XLS, RTF, TXT.

Clean Excel export: One of the most valuable features that DJ provides is exporting plain reports to excel, with no data formatting, no page break, etc. This is very valuable for end users that use this report to create dynamic tables in Excel, creating these reports just with Jasper Reports can demand a lot of design time.

Integration with popular frameworks:

  • Struts 2 and WebWork are supported out of the box.
  • Grails plug-in is soon to be released.

package com.javamagic.util;

import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperPrint;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import ar.com.fdvs.dj.core.DynamicJasperHelper;
import ar.com.fdvs.dj.core.layout.ClassicLayoutManager;
import ar.com.fdvs.dj.domain.DynamicReport;
import ar.com.fdvs.dj.domain.builders.ReflectiveReportBuilder;

public class DynamicJasperReport extends HttpServlet  {

private static final long serialVersionUID = 1L;

public String getDynamicJasperReport(String query, String title, String fileName, HttpServletResponse resp){

Session session = null;
String result = "";
try {

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();

List list = session.createQuery(query).list();

DynamicReport dynamicReport = new ReflectiveReportBuilder (list).build();

JasperPrint jasperPrint = DynamicJasperHelper.generateJasperPrint(dynamicReport, new ClassicLayoutManager(), list);

resp.setHeader("Content-Disposition", "attachment;filename=\""+fileName+".pdf\"");

JasperExportManager.exportReportToPdfStream(jasperPrint, resp.getOutputStream());

result = "success";
} catch (Exception e) {
System.out.println("Error : "+e);
result = "error";
} finally {
if (session != null) {

return result;


Now we can call that  Dynamic Jasper function from any action/servlet.

public String getDynamicJasper(){
 String result = "";

 String query = "from ExamPaper";
 String title = "List of Exam Paper";
 String fileName = "ExamPaperList";

DynamicJasperReport  djr = new DynamicJasperReport();

result = djr.getDynamicJasperReport(query, title, fileName, getResponse());
 System.out.println("result : "+result);

}catch(Exception e){
 System.out.println("Error in getDynamicJasper : "+e.getMessage());
 result = "error";
 return result;

Hope you will enjoy. Cheers…. 🙂


JAVA / JDK Version History

Java Version History

JDK 1.0 (january 23, 1996) oak
– Initial release

JDK 1.1 (february 19, 1997)
– Retooling of the AWT event model
– Inner classes added to the language
– JavaBeans

J2SE 1.2 (December 8, 1998) playground
This and subsequent releases through J2SE 5.0 were rebranded retrospectively Java 2 & version name “J2SE”
(Java 2 platform, Standard edition) replaced JDK to distinguish the base platform from
J2EE (java 2 platform, enterprise edition) and J2ME (java 2 platform, micro edition).

– Strictfp keyword
– Reflection
– Swing api integration into the core classes
– JVM equipped with a jit compiler
– Java plug-in
– Java IDL
– An IDL implementation for corba interoperability
– Collections Framework

J2SE 1.3 (may 8, 2000) kestrel
– Hotspot jvm included
– JavaSound
– JNDI included in core libraries
– Java platform debugger architecture (jpda)
– RMI was modified to support optional compatibility with corba

J2SE 1.4 (february 6, 2002) merlin
– assert keyword
– Regular expressions
– Exception chaining (allows an exception to encapsulate original lower-level exception)
– Internet protocol version 6 (IPV6) support
– Non-blocking nio (new input/output)
– Logging API
– Image i/o api for reading and writing images in formats like jpeg and png
– Integrated XML parser and XSLT processor (JAXP)
– Integrated security and cryptography extensions (JCE, JSSE, JAAS)
– Java web start

J2SE 5.0 (september 30, 2004) tiger [originally numbered 1.5]
– Generics: provides compile-time (static) type safety for collections and eliminates the need for most typecasts (type conversion).
– Metadata: also called annotations; allows language constructs such as classes and methods to be tagged with additional data, which can then be processed by metadata-aware utilities.
– Autoboxing/unboxing: automatic conversions between primitive types (such as int) and primitive wrapper classes (such as integer).
– Enumerations: the enum keyword creates a typesafe, ordered list of values (such as day.monday, day.tuesday, etc.). Previously this could only be achieved by non-typesafe constant integers or manually constructed classes (typesafe enum pattern).
– Swing: new skinnable look and feel, called synth.
– Var args: the last parameter of a method can now be declared using a type name followed by three dots (e.g. Void drawtext(string… Lines)). In the calling code any number of parameters of that type can be used and they are then placed in an array to be passed to the method, or alternatively the calling code can pass an array of that type.
– Enhanced for each loop: the for loop syntax is extended with special syntax for iterating over each member of either an array or any iterable, such as the standard collection classesfix the previously broken semantics of the java memory model, which defines how threads interact through memory.
– Automatic stub generation for rmi objects.
– Static imports concurrency utilities in package java.util.concurrent.
– Scanner class for parsing data from various input streams and buffers.
– Assertions
– StringBuilder class (in java.lang package)
– Annotations

Java SE 6 (december 11, 2006) mustang
sun replaced the name “J2SE” with java se and dropped the “.0” from the version number.
Beta versions were released in february and june 2006, leading up to a final release that occurred on december 11, 2006.
The current revision is update 14 which was released in may 2009.

– Support for older win9x versions dropped.
– Scripting lang support: Generic API for integration with scripting languages, & built-in mozilla javascript rhino integration
– Dramatic performance improvements for the core platform, and swing.
– Improved web service support through JAX-WS JDBC 4.0 support
– Java compiler API: an API allowing a java program to select and invoke a java compiler programmatically.
– Upgrade of JAXB to version 2.0: including integration of a stax parser.
– Support for pluggable annotations
– Many GUI improvements, such as integration of swingworker in the API, table sorting and filtering, and true swing double-buffering (eliminating the gray-area effect).

Java se 6 update 10
A major enhancement in terms of end-user usability.
– Java Deployment Toolkit, a set of javascript functions to ease the deployment of applets and java web start applications.
– Java Kernel, a small installer including only the most commonly used jre classes. Enhanced updater.
– Enhanced versioning and pack200 support: server-side support is no longer required.
– Java quick starter, to improve cold start-up time.
– Improved performance of java2D graphics primitives on windows, using direct3D and hardware acceleration.
– A new Swing look and feel called NIMBUS and based on synth.
– Next-generation java plug-in: applets now run in a separate process and support many features of web start applications.

Java se 6 update 12
This release includes the highly anticipated 64-bit java plug-in (for 64-bit browsers only), windows server 2008 support,
and performance improvements of java and JAVAFX applications.


Java SE 7

  • Swing
  • IO and New IO
  • Networking
  • Security
  • Concurrency Utilities
  • Rich Internet Applications (RIA)/Deployment
    • Requesting and Customizing Applet Decoration in Dragg able Applets
    • Embedding JNLP File in Applet Tag
    • Deploying without Codebase
    • Handling Applet Initialization Status with Event Handlers
  • Java 2D
  • Java XML – JAXP, JAXB, and JAX-WS
  • Internationalization
  • java.lang Package
    • Multithreaded Custom Class Loaders in Java SE 7
  • Java Programming Language
    • Binary Literals
    • Strings in switch Statements
    • The try-with-resources Statement
    • Catching Multiple Exception Types and Rethrowing Exceptions with Improved Type Checking
    • Underscores in Numeric Literals
    • Type Inference for Generic Instance Creation
    • Improved Compiler Warnings and Errors When Using Non-Reifiable Formal Parameters with Varargs Methods
  • Java Virtual Machine (JVM)
    • Java Virtual Machine Support for Non-Java Languages
    • Garbage-First Collector
    • Java HotSpot Virtual Machine Performance Enhancements
  • JDBC