views:

33

answers:

1

when i am compiling a jrxml file created using ireport and exporting it how to export this page in a particular section of a jsp page I searched a lot in google i am not gettting any solutions related to my requirements

if i am writing something in the jsp page nothing is showing only the report getting visible

my code in jsp page is:--

<%@ page language="java" import="net.sf.jasperreports.engine.*" %>
<%@ page language="java" import="net.sf.jasperreports.engine.export.*" %>
<%@ page import="java.sql.*,java.io.*" %>

<%
String filename = request.getParameter("filename");
String reporttype = request.getParameter("reporttype");
System.out.println(filename);
System.out.println(reporttype);
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/kms","root", "root");
System.out.println("Connection Established");

String path = application.getRealPath("/");
System.out.println(path);
JasperPrint jasperPrint = JasperFillManager.fillReport(path + "/reports/" + filename, null, con);


System.out.println("Report Created...");

OutputStream ouputStream = response.getOutputStream();
JRExporter exporter = null;

if( "pdf".equalsIgnoreCase(reporttype) )
{
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline; filename=\"file.pdf\"");

exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
}

try
{
exporter.exportReport();
}
catch (JRException e)
{
throw new ServletException(e);
}
finally
{
if (ouputStream != null)
{
try
{
ouputStream.close();
}
catch (IOException ex)
{
    System.out.println("exception thrown");
}
}
}
%>
A: 

Create another JSP which contains an <iframe> or <object> whose src points to the URL of the report and then open that JSP in browser instead.

<iframe src="report.jsp?filename=foo&reporttype=pdf"></iframe>

This way you have the possibility to write another template text around the <iframe>.

<!DOCTYPE html>
<html lang="en">
    <head><title>Report</title></head>
    <body>
        <h1>Here is your report</h1>
        <iframe src="report.jsp?filename=foo&reporttype=pdf"></iframe>
        <p>Was it nice, huh?</p>
    </body>
</html>

Note that sending a binary file by JSP this way may corrupt the binary content due to template text (whitespace, newline) generated by JSP. Rather do it in a Servlet class. It's just a matter of moving all the code into the doGet() method, mapping the Servlet class in web.xml and calling it instead of the JSP.

<iframe src="reportServlet?filename=foo&reporttype=pdf"></iframe>
BalusC
sir my reqiurement is not to use the iframe.
krishna
You can also use `<object>`. If that is also not an option at all, then you're doomed. All I can do is to wish you good luck with the job and recommend you to look for another employer which doesn't make silly requirements.
BalusC