Jump to content
JasperReports Library 7.0 is now available ×

Load report in jsp


Recommended Posts

By: Wesley Ng - abrabra

Load report in jsp

2003-04-09 05:22

This is my jsp, can someone help me to verify what goes wrong? I keep getting java.lang.IllegalStateException

 

<%@ page language="java" %>

<%@ page import="java.util.*" %>

<%@ page import="java.io.*" %>

<%@ page import="com.wes.business_logic.Report" %>

<%

Report test = new Report();

try

{

test.setTitle("CAMS AGENCY Custom REPORT");

test.setPageHeader("CAMS Custom Report");

 

byte[] bytes = test.generateBrowser();

response.setContentType("application/pdf");

response.setContentLength(bytes.length);

 

ServletOutputStream ouputStream = response.getOutputStream();

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

ouputStream.flush();

//ouputStream.close();

}

catch (Exception e)

{

out.println(e);

}

 

%>

 

Thanks in advance!

 

 

 

 

By: Chuck Deal - cdeal

RE: Load report in jsp

2003-04-09 05:49

I don't know if this will fix your problem, but I do know that you can NOT use response.getOutputStream() and out in the same jsp.

 

 

 

 

By: Adrian Jackson - iapetus

RE: Load report in jsp

2003-04-09 06:37

It's the problem alright (or part of it), but I don't see there's an easy way of fixing it: JSPs just aren't made for this sort of job - Servlets would be more appropriate here.

 

You can't have scriptlet code in a JSP that tries to change the header information, which is exactly what's happening in this code sample: it's trying to set the content length and type after the document body has already started to be sent, which is what leads to the exception.

 

 

 

 

By: Chuck Deal - cdeal

RE: Load report in jsp

2003-04-09 07:08

The following is a modified version of Teodor's JSP sample. This works like a charm for me... The ppage is just one big scriptlet.

 

You may have to cut-n-paste the code into a text editor to read it properly. Also, some of the code near the top is used for configuration (you can ignore that).

 

=====================

 

 

<%

/*

* IMPORTANT!!!: You cannot use both out and response.getOutputStream on the same page

* Therefore, if any errors are cause by this page, verify that the generated

* .java file does not USE out to send output to the sreen.

*/

 

/* =======================================================

PAGE: jasper_reports.jsp

 

DESCRIPTION: This is the Jasper Reports JSP to disply PDF

 

REVISION HISTORY:

CR Date Author Comment

2204 06DEC2002 Chuck Deal Creation

========================================================*/

//System.out.println("jasper_reports.jsp");

 

ServletOutputStream outStrm = null;

String jasperPath = application.getRealPath("/JasperReports/");

String jasperReport = (request.getParameter("jasperReport") != null ? request.getParameter("jasperReport") : "");

String jasperParameters = (request.getParameter("jasperParameters") != null ? request.getParameter("jasperParameters") : "");

String jasperType = (request.getParameter("jasperType") != null ? request.getParameter("jasperType") : "PDF");

byte[] outputBytes = null;

 

com.csc.aims.reports.JasperController jc = new com.csc.aims.reports.JasperController();

jc.setJasperReport(jasperPath + jasperReport);

jc.setJasperParameters(jasperParameters + "|ReportPath~" + jasperPath);

//System.out.println(jc.getJasperParameters());

 

try {

// PDF Format

if (jasperType.equals("PDF")) {

response.setContentType("application/pdf");

outputBytes = jc.getPDF();

}

// HTML Format

// else if (jasperType.equals("HTML")) {

// }

// Rich Text Format

// else if (jasperType.equals("RTF")) {

// }

// Excel format

// else if (jasperType.equals("XLS")) {

// }

}

catch (Exception e) {

response.setContentType("text/html");

 

outStrm = response.getOutputStream();

outStrm.println("<html>");

outStrm.println("<head>");

outStrm.println("<title>JasperReports</title>");

outStrm.println("</head>");

 

outStrm.println("<body>");

 

outStrm.println("JasperReports encountered this error :");

outStrm.println("<pre>");

 

e.printStackTrace();

 

outStrm.println("</pre>");

 

outStrm.println("</body>");

outStrm.println("</html>");

 

return;

}

 

if (outputBytes != null && outputBytes.length > 0) {

response.setContentLength(outputBytes.length);

 

outStrm = response.getOutputStream();

outStrm.write(outputBytes);

outStrm.flush();

}

else {

response.setContentType("text/html");

 

outStrm = response.getOutputStream();

outStrm.println("<html>");

outStrm.println("<head>");

outStrm.println("<title>JasperReports</title>");

outStrm.println("</head>");

 

outStrm.println("<body>");

 

outStrm.println("Empty response. Contact AIMS System Administrators.");

 

outStrm.println("</body>");

outStrm.println("</html>");

 

return;

}

%>

 

 

 

 

By: Frida Gen - fgen

RE: Load report in jsp

2003-07-23 08:32

Hello Charles,

In your sample(I really like it) you include

com.csc.aims.reports.JasperController

 

that is not a part of any Api classes

in order to use you sample can you please send this class in case if it not link to some more classes

Thanks Frida

 

 

 

 

By: Chuck Deal - cdeal

RE: Load report in jsp

2003-07-24 04:59

Well, the JasperController class is really just a wrapper for the call to JasperRunManager.runReporttoPDF(). I use the controller to parse parameters and load the JasperDesign objects.

 

My problem is that the JasperController class and classes that it uses are proprietary. So, I'd have to rip a bunch of things out of the class and by doing so, some things may not make sense.

 

I could make an attempt if you would still like to see a skeletal structure of the class.

 

Chuck

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...