gif animé ordinateur

dimanche 30 septembre 2012

Define and add a new JNDI object to JBoss context



Lors de mes derniers développements Java/j2ee, j’ai été amené à configurer et à ajouter des objets JNDI dans le serveur d’application JBOSS.
Cela m’a pris tout de même, une demi-journée de recherche, de configuration puis de teste, c’est pourquoi j’ai décidé de partager ces informations avec tous ceux que cela peut intéresser (des développeurs J2ee en l’occurrence).

Alors, voici ce que dit la documentation JBOSS :

JNDI Binding Manager

The JNDI binding manager service allows you to quickly bind objects into JNDI for use by application code. The MBean class for the binding service is org.jboss.naming.JNDIBindingServiceMgr. It has a single attribute, BindingsConfig, which accepts an XML document that conforms to the jndi-binding-service_1_0.xsd schema. The content of the BindingsConfig attribute is unmarshalled using the JBossXB framework.

Exemple :
Monsieur Carlo Bertoldi a mis en ligne un super tutorial  pour mieux illustrer ça, en voici le contenu :
In the last week I’ve had to deal with configuration management for the J2EE application I’m working at the moment. As you may have guessed, I chose the JNDI path. Since I’ve spent an insane amount of time, i.e. more than 5 minutes ;), googling for a solution, I’m gonna share what I discovered. Actually, it’s quite simple, we just need to instantiate a managed bean. Let’s create a file to define one java.lang.String. Create a file named pizza-service.xml in your server deploy directory. For the default configuration it should be $JBOSS_HOME/default/deploy. Filename must end with the suffix “-service”, otherwise JBoss will ignore it.
Insert the following content :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE server PUBLIC "-//JBoss//DTD MBean    Service    4.0//EN"
    "http://www.jboss.org/j2ee/dtd/jboss-service_4_0.dtd">
<server>
    <mbean code="org.jboss.naming.JNDIBindingServiceMgr" name="jboss.apps:name=pizzeria">
        <attribute name="BindingsConfig" serialDataType="jbxb">
            <jndi:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:jndi="urn:jboss:jndi-binding-service:1.0"
xs:schemaLocation="urn:jboss:jndi-binding-service resource:jndi-binding-service_1_0.xsd">
                <jndi:binding name="java:pizzeria/capricciosa">
                    <jndi:value trim="true" type="java.lang.String">
                        Hello, JNDI
                    </jndi:value>
                </jndi:binding>
                <jndi:binding name="java:pizzeria/margherita">
                    <jndi:value trim="true" type="java.net.URL">
                        http://localhost:8080/capricciosa
                    </jndi:value>
                </jndi:binding>
            </jndi:bindings>
        </attribute>
    </mbean>
</server>

This binds the text string "Hello, JNDI!" under the JNDI name java:pizzeria/capricciosa. An application would look up the value just as it would for any other JNDI value. The trim attribute specifies that leading and trailing whitespace should be ignored. The use of the attribute here is purely for illustrative purposes as the default value is true.
With this configuration, you can access the the string value “Hello, JNDI”  from within the JBoss VM using the following code fragment :
 
InitialContext ctx  = new InitialContext();
String text = (String) ctx.lookup("java:pizzeria/capricciosa");
 
Once you saved the file, JBoss should reload it automatically.
This is all it takes. If you want to separate the JNDI definitions in multiple files, just change the name attribute of the mbean definition. With this solution you can easily handle multiple configurations for your environments (test, staging etc.)

Bind a local file system directory into the JBoss JNDI namespace.

This configuration describes binding a local file system directory E:/DEV_TOOLS/TEMP/local.properties into the JBoss JNDI namespace under the name external/fs. To do so, we need to add in same xml file pizza-service.xml above, a new <mbean></mbean> XML tag like below:

<mbean code="org.jboss.naming.ExternalContext" name="jboss.jndi:service=ExternalContext,jndiName=external/fs">
<attribute name="JndiName">external/fs</attribute>
<attribute name="Properties">
java.naming.factory.initial=com.sun.jndi.fscontext.RefFSContextFactory
java.naming.provider.url=file:///E:/DEV_TOOLS/TEMP
</attribute>
<attribute name="InitialContext">
javax.naming.directory.InitialDirContext</attribute>
<attribute name="RemoteAccess">false</attribute>
</mbean>

Note that the use the Sun JNDI service providers, which must be downloaded from internet.  The provider JARs com.sun.jndi.fscontext.jar and java.naming.provider (providerutil.jar) should be placed in the server configuration lib directory.
Those two jars are available to download manually at http://www.docjar.com
This how to access programmatically this file in your code:

Context externalFsContext =(Context)initialContext.lookup("external/fs");
java.io.File  f = (File) externalFsContext.lookup(local.properties);
Properties properties = new java.util.Properties();
properties.load(new FileInputStream(f));
String urlEnv = properties.getProperty("my.env.url");

Assume that your local file local.properties countains property key my.env.url with the value you want, example: “Hello, JNDI”

For more questions, this internet link will help:
http://docs.jboss.org/jbossas/jboss4guide/r4/html/ch3.chapter.html

Hope you'll enjoy.
Mohammad, the blog owner.

dimanche 22 juillet 2012

JAAS Security on JBoss application server

I'm actually developing a Jboss web application, and using Java JAAS API to secure my web site. So I would like to share this tutorial with all the ones that this may help.
First, big up to the original author of this tutorial, Mister Nima Goodarzi, thank you for publishing and sharing your own experience with other anonymous developers. So here is it :

Introduction
 
A few days ago I was proposed to develop an airline ticketing system using JavaEE
platform. For this system I decided to use EJB3 and JSF running on JBoss application
server.
As long as security is a vital concern in such applications, I decided to use JAAS (Java
Authentication and Authorization Service) to implement authentication and
authorization.
After searching for the required configurations to implement a JAAS based security
on JBoss, I couldn’t find anything useful, even in the JBoss documents! (JavaEE
developers are not very unfamiliar with this).
It took a while for me to find all the required settings and run my project under JAAS
technology on the JBoss application server, so I decided to share my knowledge and
document it, hope to be useful for somebody.
For this project I used EJB3.0, JSF 1.2, JBoss AS 4.2.3 GA

As long as we keep our users information in database, we need to setup required
tables to store users information.
For this example we need a table named user with two columns, username and
password to keep users authentication information and a table named user_role
with two columns, user and role to keep users authorization information. 
 Step‐By‐Step Guide
First Step: Define Application Policy
As the first step we need to define a security domain for our project, security domain
is the JNDI name of the security manager interface implementation that JBoss uses
for the EJB and web containers. This is an object that implements both of the
AuthenticationManager and RealmMapping interfaces.
To define our own security domain in JBoss, we need to define a new application
policy. For this reason we modify the login‐config.xml file under
<JBOSS_HOME>/server/<PROFILE>/conf directory. For example if you have installed
your JBoss application server in the C:\ partition and you are using the default
profile, the path of the login‐config.xml file should be:

 C:\jboss‐4.2.3.GA\server\default\conf\login‐config.xml

To define a new application policy, you need to add a new <application‐policy>
element to the login‐config.xml.
This is my <application‐policy> element:

<application‐policy name="airbus">
<authentication>
<login‐module code="org.jboss.security.auth.spi.DatabaseServerLoginModule"
flag="required">
<module‐option name="dsJndiName">java:/airbusDS</module‐option>
<module‐option name="principalsQuery">
select password from user where username=?
</module‐option>
<module‐option name="rolesQuery">
select role,'Roles' from user_role where user=?
</module‐option>
<module‐option name="hashAlgorithm">MD5</module‐option>
<module‐option name="hashEncoding">base64</module‐option>
</login‐module>
</authentication>
</application‐policy>

 
Now we describe each property in the above <application‐policy> element: 

name (airbus): This is the name of this application‐policy (airbus is my project name)
which will be used later while defining your security domain.
dsJndiName (java:/airbusDS): As long as we store our user’s information in
database, we need to check their credentials against database to
authenticate/authorize them. The java:/airbusDS here is the JNDI name of the
datasource pointing to my database.
principalsQuery: This is the SQL Query that is used to get the principals of the user.
Here, we get the password of the user based on the provided username.
rolesQuery: This query is used to get roles of the provided username.
hashAlgorithm (MD5): This is the used encryption algorithm for the user’s password
in database. In this example we encrypt our user passwords using MD5 encryption
algorithm, so the JBoss authenticator must encrypt the provided password using the
same algorithm before comparing it with the actual password.
hashEncoding (base64): This is the encoding used to transform the user password
data into a 64 bit string.


Second Step: Create Security Domain
After defining the application policy, you need to define the security domain, which
has been introduced in the first step.
To define a security domain, you need to create a file named jboss‐web.xml in the
WEB‐INF directory of your web application.
Example of jboss‐web.xml:


<?xml version="1.0" encoding="UTF‐8"?>
<jboss‐web>
<security‐domain>java:/jaas/airbus</security‐domain>
</jboss‐web>

 
airbus in the above example is the name of the defined application policy in the first step.


Third Step: Secure The Application
 
In this step we secure the web application. For this reason we need to modify the
web.xml file in the WEB‐INF directory.
These are changes need to apply to the web.xml file:
1‐ Authentication:
We should tell JBoss to authenticate users before allowing them to enter the
application. This is done by adding <login‐config> element to the web.xml.


<login‐config>
<auth‐method>FORM</auth‐method>
<form‐login‐config>
<form‐login‐page>/login.jsp</form‐login‐page>
<form‐error‐page>/loginfail.jsp</form‐error‐page>
</form‐login‐config>
</login‐config>

 
In this example we tell JBoss that we need a form‐based authentication
(redirects users to our own login form). login.jsp is the designed login page
and if the authentication fails, users are redirected to loginfail.jsp. 

2‐ Create Login Page
Login page is a very simple JSP page with a form where the action of the form
is set to j_security_check and a text box, j_username for username and a
password box, j_password for Password.
3‐ Secure Web Resources:
Now we define our secured resources and required roles to access them.
This is done by adding <security‐constraint> element to web.xml.


<security‐constraint>
<web‐resource‐collection>
<web‐resource‐name>AdminPages</web‐resource‐name>
<url‐pattern>/faces/admin/*</url‐pattern>
</web‐resource‐collection>
<auth‐constraint>
<role‐name>administrator</role‐name>
<role‐name>supervisor</role‐name>
</auth‐constraint>
</security‐constraint>

 
In this example, we define all the resources under /admin directory as
secured resources and only users with the administrator or supervisor roles
are allowed to access these resources. We can define as many resources as
we need in the same way.
Note that when you define a set of resources as secured resources, none of
these resources are available for users out of the allowed roles. For example
in the example above, even images and stylesheets in the admin directory
are blocked for the users without administrator or supervisor role.
You can use HttpServletRequest isCallerInRole(String roleName) method to
see whether the logged in user has the specified role or not.


4‐ Secure EJB Methods:
The next step is to secure EJB methods; means that only allowed users can
call a secured EJB method.
In EJB3 we can use annotations to secure methods.
First, we should annotate the EJB class with @SecurityDomain("<Security
Domain Name>")
annotation.
In our example we use @SecurityDomain("airbus") annotation which, airbus
is the name of our security domain.
Then we annotate methods with @RolesAllowed({"<Role Name>"}) which,
Role Name is the allowed roles to call this method. We can also use
@PermitAll to allow every body to access the method or @DenyAll to deny
any access to the method.
You can use EJB Context isCallerInRole(String roleName) method to see
whether the logged in user has the specified role or not.
This is an example of a secured EJB Session Bean:


@Stateless
@SecurityDomain("airbus")
public class BaseServiceImpl implements BaseService {
static Logger logger = Logger
.getLogger("my.com.airbus.service.impl.BaseServiceImpl");
@PersistenceContext
private EntityManager em;
@PermitAll
public Object findById(Class clazz, Long id) {
logger.info("Find class: " + clazz.toString() + " By ID: " + id);
return em.find(clazz, id);
}
@RolesAllowed({"administrator", "supervisor"})
public void remove(Object obj){
logger.info("Delete class: " + obj.getClass().getName());
obj = em.merge(obj);
em.remove(obj);
}
}

 
In this example everybody is allowed to call the findById method, but only
administrators and supervisors can call the remove method.


Once again. greet thanks to Mister Nima Goodarzi.

jeudi 1 mars 2012

Jasper Reports - working with beans and sub report

Great thinks to Mr. Nasir Qureshi
Ho is the author of this jasperReport Tuto. 
 
Introduction

I recently had to research how to use JasperReports (refer. 1) to produce PDF, rich text format (RTF), and Excel reports using an ArrayList collection of Java objects as the source for the data in the report. The main Java class has as one of its attributes an ArrayList collection of another Java object. I had the challenge of figuring out how to include all the data from the nested ArrayList collection in the report. After several frustrating hours researching this issue in the very limited JasperReports documentation and on the JasperReports forums I found a hint that lead me to a solution.

Since there does not appear to be very good documentation on how to use complex collections as the data source for a JasperReport, I decided to write a blog entry that will hopefully help others in the future (and of course remind me how I did this when I have to do it again in six months).

JasperReports

If you visit the JasperReports website you can find information about what JasperReports does. Basically it's a comprehensive package of Java classes that enables you to use various sources as data used to create a variety of report types including PDF, Excel, and RTF. There is an open-source community edition of JasperReports that you can download (refer. 2). As of July 6, the version was JasperReports 3.0.0.

Acutally ( Thu Mar 01 2012), the JasperReport version is 4.5.0, here is its maven dependency:
        <dependency>
             <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports</artifactId>
            <version>4.5.0</version>
        </dependency>

After you download and unzip the community edition of JasperReports you'll have a folder named JasperReports-3.0.0. In this folder are sample applications, quick start documentation, and the jar files needed to use JasperReports. The main jar you'll need to include in your Java project is jasperreports-3.0.0.jar, located in the sub-folder dist.

There is some documentation for JasperReports available at reference 3.

iReport

In conjunction with JasperReports you can use iReport to graphically design your reports (instead of writing the report XML manually [the .jrxml file]). iReport gives you the ability to specify a data source and then drag-and-drop the fields from that data source onto the report. You can download iReport at reference 4. iReport is free to download and use. There is a Windows installer version and as of July 6, the iReport version is also 3.0.0. (same remarque, actual iReport version is 4.5.0)


Learning How To Use JasperReports and iReport

From my research there is limited free documentation available on the JasperReport website. There are some books you can purchase for learning how to use JasperReports and iReport at the JasperReport website. You can also Google JasperReports tutorial for a list of tutorials that may help you get started.

The general steps to creating a report are

1 - In iReport specify a source of the data that will be used to fill the report.
2 - Once iReport has access to this data source, you design the report.
3 - User iReport to compile that report design into a file of type.jasper.
The .jasper file can then be used by JasperReports to be filled with data which creates a .jrprint file type.
The .jrprint file type can then be used by JasperReports to create a PDF, RTF, or Excel version of the file.

An Example of Using An ArrayList As A Data Source For JasperReports

JasperReports and iReport can use data from various sources including databases and collections. For my current project, the data source is a collection of objects. To give you an example of how to use JasperReports and iReport to create a report that uses an ArrayList of objects as the data source I've prepared the following example.  Example source code in Eclipse project.

Model Classes:

We have two model classes: Person and Phone. A Person has a firstName and lastName. Since a Person may have multiple phones, a Person also has an ArrayList of Phone objects.

A Phone has a phoneType (for example "work" or "mobile") and a phoneNumber.

You can download an Eclipse Java project with the source code for Person and Phone and a test class from reference 6.

Setup the Data Source in iReport

If you examine the source code in test.TestPerson class (reference 6) you'll see that I've created a static method named getBeanCollection that returns an ArrayList of Person objects. This method is used in iReport to specify the data source and then will be used to fill the report with Person objects.

To create our report in iReport, we need to give iReport access to our class files for Person and Phone so that iReport can find their fields. Then we can drag-and-drop the fields onto a report design. So I created a jar of the model package (which includes the Person and Phone classes) and placed the jar in the iReport lib folder, which on my system is located at: C:\Program Files\JasperSoft\iReport-3.0.0\lib\.
In iReport 4.5.0, this is an easy way to add your model classes jar : go to : Tools--> Options-->Classpath --> add Jar (suppose your project is Maven one and its packagin type is Jar, so need to add the jar in Target directory afer execting mvn clean install goal). 

Now after restarting iReport, iReport will have access to the attributes of the Person and Phone class.

After starting up iReport we also need to specify the path to the class that has the getBeanCollection method, in this case that class is test.TestPerson and on my system the path to the test package is C:\coursecatalogtest\TestJasperReport\bin (location path of  TestJasperReport.class).

To setup the data source in iReport go to Data - Connections/Data Sources - New. Select JavaBeans set data source. Then specify the following:


In iReport 4.5.0, on click this icon and follow.

Name: PersonDataSource

Factory class: test.TestPerson

The static method to call to retrieve... getBeanCollection

You should be able to click on the Test button and get the message "Connection Test Successful." Click on Save to save this data source.

Create A Report in iReport

Use File - New Document and give the report a name of contacts. The click on OK

You'll now have a blank report with areas for title, pageHeader, columnHeader, detail, columnFooter, pageFooter, lastPageFooter, and summary. For our example we will just be placing static text in the title and columnHeader's area and then our Person fields in the detail area. For each Person object in the ArrayList, there will be a corresponding section in the detail area.

Accessing the Person Class Fields

To access the model class fields (in our example Person and Phone) in the report, click on Data - Report Query. The click on the JavaBean Data Source tab and enter the class name with path: model.Person. Because you created a jar file of the model package and placed it in iReport's lib folder earlier iReport can find the class and its attributes.


In iReport 4.5.0, this is done like below :



Click on the firstName, lastName, and phones attributes and then click on Add Selected Fields. Then click on OK.

Next click on View - Fields and you'll see these attributes. Drag the firstName and lastName fields to the detail area of the report. In the detail area you should see two boxes, one with $F{firstName} and one with $F{lastName}. The firstName and lastName fields (F) from each Person object in the collection will be placed into the detail area.

Save your report.

Accessing the Phone Class Fields

Because the phones attribute of the Person class is an ArrayList containing Phone objects, we will need to handle displaying all the Phone objects for each Person by using a sub-report. Click on Edit - Insert Element - Subreport. Use the cross-hair cursor to drag a rectangle in the detail area below where you placed the firstName and lastName fields.

With the create new report option checked click on Next. The Connection/Data Source should be PersonDataSource and the JavaBean class is model.Phone. Click next.

You should see the Phone class fields, phoneNumber and phoneType. Select both of those and click on the arrow to move them from the left box to the right box. Click Next.

Select either columnar layout or tabular layout and report format (I choose Tabular layout and classicT.xml). Click Next.

Click Finish.

(in iReport 4.5.0, this is done by drag-and-drop of subreport icon from right menu "palette", and then click next) 

You should see a subreport with the $F{phoneNumber} and $F(phoneType) fields in the detail area and the static text phoneNumber and phoneType in the columnHeader area of the subreport.

Save your reports.

Specify the Data Source for the Sub Report

Double click on the contacts.jrxml in the Files window to return to the main report. In the main report will be a sub-report icon inside the rectangle. Click on the sub-report, the right menu properties countain the parameters to configure (if it's not displayed, open header menu window, then select the one with Ctl+shift+7).
In the connection type of the properties menu, select "Use data source expression."



Click the icon in the row "Data source expression ", that is just below the drop down box. In the expression editor, delete any text in the window and type in the following:
new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{phones}). What this means is that for the sub-report you want to use a new bean collection with the phones field of the Person class as the source of the beans.


Add Title and Column Headings

You can add static text to any area of the report by draging and droping a "static Text" icon from palette right menu (equivalent to ctrl+shift+8 in top menu "window")  .Do so and then using the cross-hair cursor draw a rectangle text area in the title area of the report. Then double click on the rectangle and type in a title (eg "Contacts"). You can then use teh font family and size drop downs to style the text. Do the same procedure to add static text to the column headings area (eg "Name and Phones").

Test the Report

To test the report, you need to compile the main and sub-report and then execute the report.

In iRport 4.5.0, to compile your main report including its sub-report, just on click on this icon at the top of the report design.

Before compiling the report, set the location of where you want the compiled files to be placed. Click on Options and then settings and then click on the compiler tab. Click on the browse button and select a folder where you want the to save the compiled .jrxml report files (which are the .jasper files). I choose "C:\JasperReports".

Click on Build - Compile to compile the main contacts.jrxml file and then do the same after double-clicking on the subreport file. If you look inside the folder where you told iReport to place the compiled files you should see two files: contacts.jasper and contacts_subreport0.jasper. The .jasper files are the ones JasperReports can use to fill with data from the data source.

Double click on contacts.jrxml in iReport to open that file back up. The click on Build - Execute (with active connection). In the surreport_dir parameter window enter the folder where you told iReport to save the compiled files (in my example "c:\JasperReports\").

The iReport Jasper viewer should open up with all the Person objects displayed in the report. For each Person object there should be Phone objects displayed.

Summary

We created a very simple main and sub reports using an ArrayList of Person objects. The sub-report is tied to each Person object through the phones ArrayList (a collection of Phone objects) that is an attribute of each Person object.

iReport can be used to create very complex reports. Each report element can be styled with a specifc font, size, color, etc.

What's Next?

In part 1, I covered how to create a JasperReport report using iReport. That report's data source is an ArrayList of Person objects. Each Person object also has an ArrayList of Phone objects. In part 2, I explain how to use the JasperReport report in a Java application. You can download all the source code (Eclipse Java project).

Creating A Filled Report

In part 1, I created a main and sub-report and then compiled those reports into two .jasper files stored in c:\jasperreports\. JasperReports uses the .jasper files to fill with data and create a .jrprint file.

To give your Java application access to the JasperReport classes, you'll need to include these jar files:

jasperreports-3.0.0.jar (located in \jasperreports-3.0.0\dist\ folder)

commons-beanutils-1.7.jar (located in \jasperreports-3.0.0\lib\ folder)

commons-collections-2.1.jar (located in \jasperreports-3.0.0\lib\ folder)

commons-logging-1.0.2.jar (located in \jasperreports-3.0.0\lib\ folder)

itext-1.3.1.jar (located in \jasperreports-3.0.0\lib\ folder)

poi-3.0.1-FINAL-20070705.jar (located in \jasperreports-3.0.0\lib\ folder)

So step 1 is to fill the .jasper file with data. These statements do that:
/*
* Setup the parameters and their values
* needed by the .jasper file
*/
Map parameters = new HashMap();
parameters.put("SUBREPORT_DIR", "c:/JasperReports/");
JasperFillManager.fillReportToFile("C:/JasperReports/contacts.jasper", parameters,
new JRBeanCollectionDataSource(TestPerson.getBeanCollection() ) );

The first two statements create a Map containing the parameter name (the key) and the parameter's value (the value). For this JasperReport we need to send it the value of the subreport_dir (the directory where the sub-report is stored).

The we can use the JasperFillManager class's fillReportToFile method to fill the .jasper file with data. This method takes three arguments, the .jasper file (we created this using iReport), the parameters to send to that .jasper file, and the data source.

Note that our data source is using the JRBeanCollectionDataSource class, which has a constructor that takes an ArrayList of bean objects. We create that ArrayList by calling the static getBeanCollection method of our class TestPerson. This was the same data source we used in part 1 to create our report in iReport.

Converting A Filled Report to A PDF

The following code uses the filled report, which is stored in contact.jrprint, to create a PDF.
JasperExportManager.exportReportToPdfFile("C:/JasperReports/contacts.jrprint");

In folder c:\jasperreports\ should now be a PDF named contacts.pdf containing all the data returned by the TestPerson.getBeanCollection method.

Converting A Filled Report to An Rich Text Format (RTF) File

To create an RTF file (which can be opened by Word or other word-processing software) use the following statements.
File sourceFile = new File("C:/JasperReports/contacts.jrprint");

JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);

File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".rtf");

JRRtfExporter exporter = new JRRtfExporter();

exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());

exporter.exportReport();

The above code creates a JasperPrint object using our .jrprint file (which is filled with data from our data source). Then using a JRRtfExporter object it exports the .jrprint file to a RTF file with the same name but extension of .rtf.

After running the above code you should have a contacts.rtf file in the c:\jasperreports\ folder.

Converting A Filled Report to An Excel File

To create an Excel file use the following statements.
destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".xls");

JRXlsExporter xlsExporter = new JRXlsExporter();

xlsExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
xlsExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
xlsExporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);

xlsExporter.exportReport();

The code above is similar to how we created the RTF file, except it uses class xlsExporter to export the .jrprint file to an Excel file type.

Summary

The hardest part of using JasperReports is creating the report. Once you have a report created, you can fill that report with data from a data source. In this example the data source is a collection of Person objects. There are other types of data sources including databases.

Once you have filled your .jasper report (which was created by compiling the .jrxml file in iReport) and created the .jrprint file, you can use JasperReport to convert that .jrprint file to various file types including PDF, RTF, and Excel.

Once again, great thinks de Nasir Qureshi, ho is the original author of this document.
Mohammad. the blog owner.