954,574 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Calling a .jasper file from a JSP.

HI
I had generate a report with IREPORT 3.7 . I have now a .jasper file.
I would want to be able to execute (show) it with a jsp file or servlet.
My generated report not use sql queries, only it need 2 parameters. i think to pass these parameters from 2 text input in other jsp file.
The code that I am trying is :

<%-- 
    Document   : reporte
    Created on : 01/09/2010, 01:31:42 PM
    Author     : JHON
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="net.sf.jasperreports.engine.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="net.sf.jasperreports.view.JRViewer" %>
<%@ page import="com.itextpdf.text.*"%>
<%@ page import="com.itextpdf.text.pdf.*"%>
<%@ page import="java.util.HashMap" %>
<%


            /*Parametros para realizar la conexión*/
            Connection conexion;
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            conexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/gae", "root", "");
            /*Establecemos la ruta del reporte*/
            File reportFile = new File(application.getRealPath("reportes//prueba1.jasper"));
            /* No enviamos parámetros porque nuestro reporte no los necesita asi que escriba cualquier cadena de texto ya que solo seguiremos el formato del método runReportToPdf*/
            Map parameters = new HashMap();
            parameters.put("cedula", "cedula");
            parameters.put("nombre", "nombre");
            /*Enviamos la ruta del reporte, los parámetros y la conexión(objeto Connection)*/
            byte[] bytes = JasperRunManager.runReportToPdf(reportFile.getPath(), parameters, conexion);
            /*Indicamos que la respuesta va a ser en formato PDF*/
            response.setContentType("application/pdf");
            response.setContentLength(bytes.length);
            ServletOutputStream ouputStream = response.getOutputStream();
            ouputStream.write(bytes, 0, bytes.length); /*Limpiamos y cerramos flujos de salida*/
            ouputStream.flush();
            ouputStream.close();
%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
    </body>
</html>


Anyone know other method without to use connection?? This code give me error in the line 32!!!

Muchas Gracias

jhonilson
Newbie Poster
8 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

There are many overloaded runReportToPdf() methods for the JasperRunManager object including the one without connection. Go through the JasperRunManager APIs for more details

parry_kulk
Junior Poster
Team Colleague
167 posts since Jan 2007
Reputation Points: 26
Solved Threads: 41
 

Hi all,
Please include
jasperreports-1.0.2.jar and jasperreports-1.0.2-applet.jar
in your WEB-INF/lib directory
to solve this issue.

You can get these jar files from "jasperreports-1.0.2\dist" which is extracted from "jasperreports-1.0.2-project.zip" file..

Amunantha
Newbie Poster
1 post since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: