Archive for Tháng Bảy, 2005

Where should I put resources (like images and font…

Where should I put resources (like images and font files)?
The report is run as a new java process that’s executed in the directory where the file is located. That means that Jasper Reports can manage absolute or relative paths because the local directory is the directory where the file is located.

Can I run reports in a web application?
Yes.I t’s very easy to write a servlet (or a jsp) with some code like the following:

File reportFile =

 new File(application.getRealPath("
Map parameters = new HashMap();
parameters.put("ReportTitle", "Address Report");
byte[] bytes =
new WebappDataSource()

ServletOutputStream ouputStream =
ouputStream.write(bytes, 0, bytes.length);

Tháng Bảy 23, 2005 at 8:55 sáng Bạn nghĩ gì về bài viết này?…
Now Java part of it here are code snippets

JasperReportHelper helper = new JasperReportHelper();
Map properties = new HashMap();
properties.put( "TITLE", "");
properties.put( "BEGIN_DATE", beginCal.getTime() );
properties.put( "END_DATE", endCal.getTime());

* Generate an ActivityReport report in PDF format.
* properties: is the map that is created above with all the
* user entered variables
* reportDesign: is the compiled file name like someRep.jasper
* fileName: ouputfileName
* outputType like pdf
public void createReport( Map properties, String reportDesign, String fileName, String outputType ) throws Exception {
InputStream is = null;
Connection conn = null;
try {
// Get an input stream to the compiled report defesign
is = this.getClass().getResourceAsStream(reportDesign);
JasperReport jasperReport = (JasperReport)JRLoader.loadObject( is );

/* Open a connection to the odbc data source entered by the user */
conn = Torque.getConnection();

if ( conn == null || conn.isClosed() ) {
throw new Exception( "Unable to get a valid connection to the database. ");
// populate the report
JasperPrint jasperPrint = JasperManager.fillReport( jasperReport, properties, conn );

if ( outputType.equalsIgnoreCase( "xml")) {
JasperExportManager.exportReportToXmlFile( jasperPrint, fileName, false );

} else if ( outputType.equalsIgnoreCase( "csv")) {
JRCsvExporter exporter = new JRCsvExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, fileName );

} else if ( outputType.equalsIgnoreCase( "xls")) {
JRXlsExporter exporter = new JRXlsExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, fileName );
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);

} else {
// Write out the pdf file
JasperManager.printReportToPdfFile( jasperPrint, fileName );

} finally {
if ( is != null ) {
try {
} catch(Exception ex ) {

try {
if ( conn != null && ! conn.isClosed() ) {
Torque.closeConnection( conn );
} catch ( Exception ex ) {

The variables put in the properties map will be available in the report xml and can be referenced as $P{BEGIN_DATE or whatever}

Tháng Bảy 12, 2005 at 12:02 chiều Bạn nghĩ gì về bài viết này?

Another Sample code for JasperReport using Servlet

JasperReport tren web

ServletContext context = this.getServletConfig().getServletContext();
File report_file = context.getRealPath(path);//get file
Map parameters = new HashMap();
parameters.put(“title”,reporttile);// set para

JasperPrint jasperPrint = null;

try {
//load jasper file
JasperReport jasperReport = (JasperReport) JRLoader
.loadObject(report_file .getPath());
//get jasperPrint instance
jasperPrint = JasperFillManager.fillReport(jasperReport,
parameters, new JREmptyDataSource());
} catch (JRException e) {
if (jasperPrint != null) {//out put
ServletOutputStream ouputStream = response.getOutputStream();

ObjectOutputStream oos = new ObjectOutputStream(ouputStream);

} else {

Tháng Bảy 11, 2005 at 11:57 sáng Bạn nghĩ gì về bài viết này?

Dùng JRResultSetDataSource?

Dùng Resultset như là JRResultSetDataSource? Hãy xem thử ví dụ sau:


String connectString = “jdbc:oracle:thin:@”;
String user = “SCOTT”;
String password = “tiger”;
Connection conn = DriverManager.getConnection(connectString, user, password);
Statement stmt = conn.createStatement();
ResultSet rs;
rs = stmt.executeQuery(“select * from customers”);
//Preparing parameters
Map parameters = new HashMap();
parameters.put(“mytitle”, “Customer Details”);
JasperFillManager.fillReportToFile(fileName, parameters, getConnection());


Tháng Bảy 5, 2005 at 9:07 sáng Bạn nghĩ gì về bài viết này?

Weeks calculation tips

To group by week you need to write a week grouping expression. This expression should assign a unique number for each week. Such a week calculation routine involves the use of java.util.Calendar and is best placed in a utility class. The routine would look like this (I am writing this from my head):

public static Integer getWeekNumber(Date date) {
Calendar calendar = Calendar.getInstance();
return new Integer(calendar.get(Calendar.WEEK_OF_YEAR) * calendar.get(Calendar.YEAR));

The second problem is to calculate the date of the first day in the week. This is also should be placed in a utility class and like the above it involves the use of the Calendar class:

public static Date getWeekStartDate(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
return calendar.getTime();

Tháng Bảy 1, 2005 at 9:04 sáng Bạn nghĩ gì về bài viết này?

Another resources for Java server-side developers

Liệt kê những resource dùng để develop Java server-side, theo từng thể loại.
Xem ở đây :

Trong đó còn có “List of free Java books for download” :

Tháng Bảy 1, 2005 at 4:24 sáng Bạn nghĩ gì về bài viết này?

JasperReport Servlet library, Jaspanese language :-(

Publish JasperReports trên Web?, muốn có paging ?

hoặc tham khảio thêm ở đây nếu rành tiếng Nhật 🙂 :

Tháng Bảy 1, 2005 at 2:28 sáng Bạn nghĩ gì về bài viết này?

Bài đăng cũ hơn

Tháng Bảy 2005
« Th6   Th8 »

Bài viết mới

RSS Jason Rudolph (GRails)

  • Lỗi: có thể dòng không tin đang không hoạt động. Hãy thử lại sau.