Hi i'm a newbie trying to learn something. I downloaded ireport and have designed my report. It connects to the database and retrives the value with no problems. When i build the report i'm able to see it as a Pdf and it works.

My question is how do i link this to a jsp page. Lets say when i click a button the pdf report appears. The only file i seem to have in the folder is classic.jrxml Thank you for your help...

Dani AI

Generated

Short practical answer for (and for , ): compile your JRXML to a compiled .jasper (either in iReport with the Compile button or at runtime with JasperCompileManager), then stream the filled report to the browser from a servlet. Put the Jasper (.jasper) file and any report resources under WEB-INF (or on the classpath) and add the JasperReports jars + dependencies into WEB-INF/lib. (jasperreports.sourceforge.net)

Minimal servlet pattern (precompiled .jasper — recommended for performance):

import net.sf.jasperreports.engine.*;
import javax.servlet.*;
import javax.servlet.http.*;
...
protected void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
  res.setContentType("application/pdf");
  res.setHeader("Content-disposition","inline; filename=classic.pdf");

  InputStream jasperStream = getServletContext().getResourceAsStream("/WEB-INF/reports/classic.jasper");
  Map<String,Object> params = new HashMap<>();
  Connection conn = /* get from DataSource (JNDI) */;
  try {
    JasperPrint jp = JasperFillManager.fillReport(jasperStream, params, conn);
    JasperExportManager.exportReportToPdfStream(jp, res.getOutputStream());
  } catch (JRException e) {
    throw new ServletException(e);
  } finally {
    if (conn != null) try { conn.close(); } catch (SQLException ignore) {}
  }
}

The example uses fillReport then exportReportToPdfStream to write binary PDF directly to the response. (javadoc.io)

Best-practice notes and quick fixes: do this from a servlet (not a JSP that may accidentally write characters before the PDF), precompile .jrxml for production to avoid compile cost on each request, use a pooled DataSource (JNDI) for DB connections, and check WEB-INF/lib for the correct JasperReports + dependency jars if you see ClassNotFound / JRException errors. If you still need to compile at runtime, use JasperCompileManager.compileReport(InputStream) before filling. (stackoverflow.com)

(Thanks to for the pointer to a tutorial — that background reading is helpful, but the servlet snippet above shows the direct, repeatable approach that resolves the “only have classic.jrxml” problem.)

Recommended Answers

All 3 Replies

Hi i'm a newbie trying to learn something. I downloaded ireport and have designed my report. It connects to the database and retrives the value with no problems. When i build the report i'm able to see it as a Pdf and it works.

My question is how do i link this to a jsp page. Lets say when i click a button the pdf report appears. The only file i seem to have in the folder is classic.jrxml Thank you for your help...

hi

I am also facing the same problem, have you got the result, if so pls help me

I am also facing the same problem, if you get the result, pls help me, or contact to <snip>

Hi,

I think the iReport article on may be helpful in this discussion.

This popular white paper is written by a software engineer from our organization Mindfire Solutions (http://www.mindfiresolutions.com).

I hope you find it useful!

Cheers,
Byapti

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.