Jump to content
Changes to the Jaspersoft community edition download ×

Problems with sending report to client

Recommended Posts

By: Darryl Baker - shakenbake

Problems with sending report to client

2002-07-28 13:21

Hi. Ok, I'll cut right to it. On my server I am able to create a JasperPrint object using JasperFillManager.fillReport. What I was trying to do then was send this serialized object to the client. The code I have in the servlet to do this is:


ObjectOutputStream oos = new ObjectOutputStream();

res. setContentType("application/octet-stream");






On the client side, the JVM is supposed to execute:



However, there is an error in the servlet before JasperPrint object is sent to client: NoClassDefFoundError: java.util.Collection


I also tried the code in letter.jsp of webapp demo, ie:






I could not get this to work either. How do you get this to work on client anyway? I know the end result is for the pdf report to come up in the browser but what do I need to do on the client to have this happen? I guess what I am basically asking for is a code snippet on both the server and the client that will get me over this hump and allow me to view a report on the client computer. Also, should I be sending a byte pdf to the client or a JasperPrint object?







By: Yong Wang - wymz

RE: Problems with sending report to client

2002-07-29 08:23

Hi, I am having the same problem. When I try to run the sample in webapp, a window would pop up asking me whether to open or save. If I select open, a text editor would pop up filled wtih PDF data. If I select save, it would save to a file called "letter.jsp" which contains PDF data. I have to rename it to "letter.pdf" in order to properly launch Acrobat reader.


Could you be kind enough forward me the resolution if you receive any? Appreciate it.



By: Eric Everman - eeverman

RE: Problems with sending report to client

2002-07-29 09:18

This sounds like a IE browser problem. You *should* be able to set the content type of a returned page to the correct type (application/acrobat????) and the browser *should* hand the data off to the appropriate application. However, IE is known for ignoring the content type header and just looking at the file extension.


So, to make your pages IE proof, be sure to return a page that has the correct content type AND the .pdf extension. I have been using a servlet to do this that serves up a .pdf file that I store in a temp directory. Here is my servlet code:



public void doPost(

HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {


File pdfFile = null; //Will reference temp created pdf file

String id = request.getParameter("id"); //id parameter used to create report


try {

//My method that creates the pdf report file

//and stores it in the context temp folder

pdfFile = createReport(id);


//Easy way to get correct content type for the pdf file

String contentType =




//Uses the OReilly Servlet Utility to send the file to client found at:


ServletUtils.returnFile(pdfFile.getPath(), response.getOutputStream());


} catch (Exception e) {

try {

//Also use OReilly Servlet Util to get a stack trace as as string. . .

String err = "An Error occured. The error type was: " +

e.getClass().getName() +

" The message was: " + e.getMessage() +

" And here is a stack trace: " + ServletUtils.getStackTraceAsString(e);







} catch (Exception ee) {/*Can't help much with this situation*/}


} finally {

if (pdfFile != null && pdfFile.exists()) pdfFile.delete();





Not sure if this addresses the original poster's question. . .


Good Luck,


Eric Everman



By: Yong Wang - wymz

RE: Problems with sending report to client

2002-07-29 11:44

Hi Eric, I tried your approach and the problem persists. I print the content type after writing it to a file and it shows "application/pdf". The length is also correct. The only difference is I am running it from a JSP while yours is in servlet. The following code is adapted from the sample webapp, do you see anything wrong? Thanks!


<%@ page errorPage="jasperError.jsp" %>

<%@ page import="dori.jasper.engine.*" %>

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

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

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

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

<%@ page import="com.oreilly.servlet.*" %>



File reportFile = new File(application.getRealPath("/JasperReportsLetter.jasper"));


Map parameters = new HashMap();

parameters.put("ReportTitle", "JasperReports Project Description");

parameters.put("BaseDir", reportFile.getParentFile());



byte[] bytes =




new JREmptyDataSource()



JasperReport jasperReport = JasperManager.loadReport(reportFile.getPath());


JasperPrint jasperPrint = JasperManager.fillReport(jasperReport, parameters, new JREmptyDataSource());


File pdfFile = new File( "C:\temp\test.pdf" );


JasperManager.printReportToPdfFile(jasperPrint, pdfFile.getPath());

String contentType = getServletContext().getMimeType(pdfFile.getPath());

System.out.println( "content type = " + contentType );

System.out.println( "content length = " + pdfFile.length() );





response.setContentLength( (int) pdfFile.length());


ServletOutputStream ouputStream = response.getOutputStream();

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

ServletUtils.returnFile(pdfFile.getPath(), ouputStream);






By: Eric Everman - eeverman

RE: Problems with sending report to client

2002-07-29 12:24

The fact that you are creating a file to be downloaded means that its must be working *close* to correctly, if not entirely perfect.


My guess is that this still a file extension problem. When you go to the jsp page in your browser is it .jsp or is it .pdf? If your browser is IE, setting the content type often doesn't matter.


To fix this, you need to map your jsp page to a url with a .pdf name in your web.xml file. Here is an example from that file:












Good luck,


Eric Everman



By: Teodor Danciu - teodord

RE: Problems with sending report to client

2002-08-05 02:26




Darryl, before going any further, you have to tell

me the exact configuration you are trying to use

for your client/server application.


Are you trying to create a desktop Java client

application that will connect to the server and read

the servlet output in order to view the report?

Or we are talking about a Web application with

the browser as a client?


Thank you,





By: Peter Kelley - yellekau

RE: Problems with sending report to client

2002-08-06 01:18

I had the same problem today and the only way I could figure out to solve it was to change the HTTP method on the calling page from a POST to a get. You get all sorts of ugly parameters on your URL but it works for me (IE6.0, Win 2K, Jasper Reports running off Webwork).



By: Yong Wang - wymz

RE: Problems with sending report to client

2002-08-06 13:51


After sipping through the archive of this bbs, I found the following header fixes my problem to some extent. The browser can now correctly launch reader to display pdf content. Setting only length and content type will not work for IE6.0. The following header also must be set for it to work.


response.setHeader ("ContentDisposition", "attachment; filename="syntax.pdf"");

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...