Solved

Apache FOP with Struts

Posted on 2004-08-19
4
1,123 Views
Last Modified: 2013-11-24
Hello Experts,
I'm trying to get my Struts Action class to generate a PDF response using Apache FOP.  I'm pretty new to FOP and not a Struts expert.  So far, what I've done in my Action class's execute() method is:

response.setContentType("application/pdf");
Driver driver = new Driver(new org.xml.sax.InputSource("foo.fo"), response.getOutputStream());
driver.setRenderer(Driver.RENDER_PDF);
driver.run();

This was taken pretty much straight from the FOP with Servlets example.
The exception I'm getting is:

java.lang.IllegalStateException: getOutputStream() has already been called for this response
      org.apache.coyote.tomcat5.CoyoteResponse.getWriter(CoyoteResponse.java:614)

I did expect to get an exception because I was just stabbing in the dark and wanted to see if I could get some kind of PDF output happening, but if anyone could either tell me whats wrong here or guide me in using FOP with Struts I'd be really grateful.

Cheers,
G.
0
Comment
Question by:blackfrancis75
  • 2
4 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 250 total points
ID: 11841106
I'm not sure you can write to the response object in a struts action...

I always put stuff like that into a servlet, and redirect the request to point to that servlet
0
 
LVL 35

Expert Comment

by:girionis
ID: 11841152
That's how we do it with Spring (where "app" is an object of type Appendix):

response.setContentType(<pdf content type>);
byte [] bytes = app.getFileContent();
response.setContentLength(bytes.length);
java.io.OutputStream os = response.getOutputStream();
os.write(bytes);
os.flush();
os.close();

and then we return null for the ModelAndView.

return null;

It should be more or less the same for struts.
0
 
LVL 5

Author Comment

by:blackfrancis75
ID: 11848644
OK I've created a servlet that does the FOP stuff and I'm redirecting now from my Struts Action class to the Servlet, but doing so generates a 405 Error with the exception:

HTTP method POST is not supported by this URL

Any ideas about this one?
0
 
LVL 5

Author Comment

by:blackfrancis75
ID: 11848737
Aha, I changed the doPost () servlet method to service() and now it works.

Thanks for the pointer Tim.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now