Something about JasperReport

1.What is JasperReports:
JasperReports is a powerful open source reporting tool that has the ability to deliver rich content onto the screen, to the printer or into HTML, XLS, PDF, CSV and XML files. It is entirely written in Java and can be used in a variety of Java enabled applications to generate dynamic content.
Its main purpose is to help creating page oriented, ready to print documents in a simple and flexible manner.

JasperReports organizes data retrieved from a relational database through JDBC according to the report design defined in an XML file. In order to fill a report with data, the report design must be compiled first.
The compilation of the XML file representing the report design is performed by the compileReport() method exposed by the dori.jasper.engine.JasperManager class.
Through compilation, the report design is loaded into a report design object that is then serialized and stored on disk (dori.jasper.engine.JasperReport). This serialized object is then used when the application wants to fill the specified report design with data. In fact, the compilation of a report design implies the compilation of all Java expressions defined in the XML file representing the report design. Various verifications are made at compilation time, to check the report design consistency. The result is a ready to fill report design that will be then used to generate documents on different sets of data.
In order to fill a report design, one can use the fillReportXXX() methods exposed by the dori.jasper.engine.JasperManager class. Those methods receive as a parameter the report design object, or a file representing the specified report design object, in a serialized form, and also a JDBC connection to the database from where to retrieve the data to fill the report.
The result is an object that represents the ready to print document (dori.jasper.engine.JasperPrint) and can be stored onto the disk, in a serialized form, for later use, or can be delivered to the printer, to the screen or can be transformed into a HTML, XLS, PDF, CSV or XML document.

JasperReports’ reports are defined in XML files, which by convention have an extension of jrxml. A typical jrxml file contains the following elements:

• jasperReport – the root element.
• title – its contents are printed only once at the beginning of the report
• pageHeader – its contents are printed at the beginning of every page in the report.
• detail – contains the body of the report.
• pageFooter – its contents are printed at the bottom of every page in the report.
• band – defines a report section, all of the above elements contain a band element as its only child element.

All of the elements are optional, except for the root jasperReport element.

2.Jasper available Plugins:
JasperAssistant is a visual report designer for JasperReports, a popular open-source reporting engine. It is built on top of the Eclipse’s plug-in architecture and its main goal is to help you create JasperReports report definition files through an intuitive graphical interface.
Benefits
• Design and layout your reports using an intuitive visual interface and avoid the need for XML editing of JasperReports templates.

• Take advantage of Eclipse IDE integration and effectively combine Java and JasperReports development.

• Efficiently locate and correct report problems using precise error indications.

• Preview your reports instantly using live data from a JDBC database connection, an XML document, JavaBeans array or a custom data source.

• Export and preview your reports in PDF, Excel, HTML, CSV and XML formats.

Find out How Much Memory Used by a Java Object

Here is some very good description of what you have to take into account to compute the (shallow) size of an java object.

The general rules for computing the size of an object on the SUN/SAP VM are :

32 bit

Arrays of boolean, byte, char, short, int: 2 * 4 (Object header) + 4 (length-field) +
sizeof(primitiveType) * length -> align result up to a multiple of 8

Arrays of objects: 2 * 4 (Object header) + 4 (length-field) +
4 * length -> align result up to a multiple of 8

Arrays of longs and doubles: 2 * 4 (Object header) + 4 (length-field) +
4 (dead space due to alignment restrictions) + 8 * length

java.lang.Object: 2 * 4 (Object header)

other objects: sizeofSuperClass + 8 * nrOfLongAndDoubleFields + 4 * nrOfIntFloatAndObjectFields +
2 * nrOfShortAndCharFields + 1 * nrOfByteAndBooleanFields -> align result up to a multiple of 8

64 bit

Arrays of boolean, byte, char, short, int: 2 * 8 (Object header) + 4 (length-field) +
sizeof(primitiveType) * length -> align result up to a multiple of 8

Arrays of objects: 2 * 8 (Object header) + 4 (length-field) +
4 (dead space due to alignment restrictions) + 8 * length

Arrays of longs and doubles: 2 * 8 (Object header) + 4 (length-field) +
4 (dead space due to alignment restrictions) + 8 * length

java.lang.Object: 2 * 8 (Object header)

other objects: sizeofSuperClass + 8 * nrOfLongDoubleAndObjectFields + 4 +
nrOfntAndFloatFields + 2 * nrOfShortAndCharFields + 1 * nrOfByteAndBooleanFields -> align result up to a multiple of 8

Note that an object might have unused space due to alignment at every inheritance level (e.g. imagine a class A with just a byte field and class B has A as it’s superclass and declares a byte field itself -> 14 bytes ‘wasted on 64 bit system).

In practice 64 bit needs 30 to 50% more memory due to references being twice as large.

String and Memory Issue

Probably most of the Java users is aware that String object is more complex than just an array of char. To make the usage of strings in Java more robust additional measures were taken – for instance the String pool was created to save memory by reusing the same String objects instead of allocating new ones. Another optimization that I want to talk about today is adding the offset and count fields to the String object instances.

Why those fields were added? Their purpose is to help to reuse already allocated structures when using some of the string functionalities – like calculating substrings of a given string. The concept is that instead of creating an new char array for a substring we could just ‘reuse’ the old one. To be exact this is what String.substring() method does: instead of copying an char array for the returned object it creates a new String reusing char[] of the old one. Only the values of offset and count fields (which indicate the beginning and the length of a new string) are changed. Because the substring operation is quite often used this mechanism helps to save a lot of memory. It is important to add that this can work only because String objects are immutable. See the following snippet:

——————————————————————
public static void sendEmail(String emailUrl) {
String email = emailUrl.substring(7); // ‘mailto:’ prefix has 7 letters
String userName = email.substring(0, email.indexOf(”@”));
String domainName = email.substring(email.indexOf(”@”));
}

public static void main(String[] args) {
sendEmail(”mailto:user_name@domain_name.com”);
}
——————————————————————
Thanks to the way substring() is implemented when we extract the email, userName and domainName three new String objects are created, but the char array is not copied. All new string variables reuse the character array from emailUrl. Thanks to that reuse for really long urls we can save approximately 2/3 of memory we would use otherwise. Great, right?
Ok… now the dark side of that optimization! Check out this snippet:

——————————————————————
public final static String START_TAG = ““;

public static String getPageTitle(String pageUrl) {
// retrieve the HTML with a helper function:
String htmlPage = getPageContent(pageUrl);

// parse the page content to get the title
int start = htmlPage.indexOf(START_TAG);
start = start + START_TAG.length();
int end = htmlPage.indexOf(END_TAG);
String title = htmlPage.substring(start, end);
return title;
}
——————————————————————
In here we are extracting from the HTML page its title – can you see the problem with this code? Looks simple and correct, right?
Now, try to imagine that the htmlPage String is huge – more than 100.000 characters, but the title of this page has only 50 characters. Because of the optimization mentioned above the returned object will reuse the char array of the htmlPage instead of creating a new one… and this means that instead of returning a small string object you get back a huge String with 100.000 characters array!! If your code will invoke getPageTitle() method many times you may find out that you have stored only a thousand titles and already you are out of memory!! Scary, right?

Of course there is an easy solution for that – instead of returning the title in line 13, you can return new String(title). The String(String) constructor is always doing a subcopy of the underlying char array, so the created title will actually have only 50 characters. Now we are safe:)
So what is the lesson here? Always use new String(String)? No… In general the String optimizations are really helpful and it is worth to take advantage of them. You just have to be careful with what you are doing and be aware of what is going on ‘under the hood’ of your code.

Java Knowledge Bank – 1

Q : Give a few reasons for using Java?
A : Java is a fun language. Let’s look at some of the reasons:
􀂃 Built-in support for multi-threading, socket communication, and memory management (automatic garbage
collection).
􀂃 Object Oriented (OO).
􀂃 Better portability than other languages across operating systems.
􀂃 Supports Web based applications (Applet, Servlet, and JSP), distributed applications (sockets, RMI. EJB etc)
and network protocols (HTTP, JRMP etc) with the help of extensive standardised APIs (Application Program
Interfaces).
Q : What are the advantages of Object Oriented Programming Languages (OOPL)?
A : The Object Oriented Programming Languages directly represent the real life objects like Car, Jeep, Account,
Customer etc. The features of the OO programming languages like polymorphism, inheritance and
encapsulation make it powerful. [Tip: remember pie which, stands for Polymorphism, Inheritance and
Encapsulation are the 3 pillars of OOPL]
Q : What is the difference between an abstract class and an interface and when should you use them?
A : In design, you want the base class to present only an interface for its derived classes. This means, you don’t want anyone to actually instantiate an object of the base class. You only want to upcast to it (implicit upcasting, which gives you polymorphic behaviour), so that its interface can be used. This is accomplished by making that class abstract using the abstract keyword. If anyone tries to make an object of an abstract class, the compiler prevents it.
The interface keyword takes this concept of an abstract class a step further by preventing any method or function implementation at all. You can only declare a method or function but not provide the implementation. The class, which is implementing the interface, should provide the actual implementation. The interface is a very useful and commonly used aspect in OO design, as it provides the separation of interface and implementation and enables you to:
􀂃 Capture similarities among unrelated classes without artificially forcing a class relationship.
􀂃 Declare methods that one or more classes are expected to implement.
􀂃 Reveal an object’s programming interface without revealing its actual implementation.
􀂃 Model multiple interface inheritance in Java, which provides some of the benefits of full on multiple
inheritances, a feature that some object-oriented languages support that allow a class to have more than one superclass.
When to use an abstract class?: In case where you want to use implementation inheritance then it is usually
provided by an abstract base class. Abstract classes are excellent candidates inside of application frameworks.
Abstract classes let you define some default behaviour and force subclasses to provide any specific behaviour.
Care should be taken not to overuse implementation inheritance .
When to use an interface?: For polymorphic interface inheritance, where the client wants to only deal with a type and does not care about the actual implementation use interfaces. If you need to change your design frequently, you should prefer using interface to abstract. CO Coding to an interface reduces coupling and interface inheritance can achieve code reuse with the help of object composition. Another justification for using interfaces is that they solve the ‘diamond problem’ of traditional multiple inheritance as shown in the figure. Java does not support multiple inheritances. Java only supports multiple interface inheritance. Interface will solve all the ambiguities caused by this ‘diamond problem’

Q : Explain the Java Collection framework?
A: The key interfaces used by the collection framework are List, Set and Map. The List and Set extends the Collection interface. Should not confuse the Collection interface with the Collections class which is a utility class.
A Set is a collection with unique elements and prevents duplication within the collection. HashSet and TreeSet
are implementations of a Set interface. A List is a collection with an ordered sequence of elements and may
contain duplicates. ArrayList, LinkedList and Vector are implementations of a List interface.
The Collection API also supports maps, but within a hierarchy distinct from the Collection interface. A Map is an object that maps keys to values, where the list of keys is itself a collection object. A map can contain duplicate values, but the keys in a map must be distinct. HashMap, TreeMap and Hashtable are implementations of a Map interface.
How to implement collection ordering? SortedSet and SortedMap interfaces maintain sorted order. The
classes, which implement the Comparable interface, impose natural order. For classes that don’t implement
comparable interface, or when one needs even more control over ordering based on multiple attributes, a
Comparator interface should be used.
Q : Give an example where you might use a static method?
A : Static methods prove useful for creating utility classes, singleton classes and factory methods Utility classes are not meant to be instantiated. Improper coding of utility classes can lead to
procedural coding. java.lang.Math, java.util.Collections etc are examples of utility classes in Java.

J2EE Overview

What Is the J2EE?
- Open and standard based platform for
- developing, deploying and managing
- n-tier, Web-enabled, server-centric, and component-based enterprise applications

What Makes Up J2EE?
- API and Technology specifications
- Development and Deployment Platform
- Standard and production-quality implementation
- Compatibility Test Suite (CTS)
- J2EE brand
- J2EE Blueprints
- Sample codes

Evolution of Enterprise Application Framework
- Single tier
- Two tier
- Three tier
– RPC based
– Remote object based
- Three tier (HTML browser and Web server)
- Proprietary application server
- Standard application server

J2EE 1.4 APIs and Technologies
- J2SE 1.4 (improved)
- JAX-RPC (new)
- Web Service for J2EE
- J2EE Management
- J2EE Deployment
- JMX 1.1
- JMS 1.1
- JTA 1.0
- Servlet 2.4
- JSP 2.0
- EJB 2.1
- JAXR
- Connector 1.5
- JACC
- JAXP 1.2
- JavaMail 1.3
- JAF 1.0

Summary
- J2EE is the platform of choice for development and deployment of n-tier, web-based, transactional, component based enterprise applications
- J2EE is standard-based architecture
- J2EE is all about community
- J2EE evolves according to the needs of the industry

Follow

Get every new post delivered to your Inbox.