Solved

Apache FOP with Struts

Posted on 2004-08-19
4
1,152 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
netstat -ano | find "8000" and taskkill /f /pid 2984 3 83
MySqlDump not dumping triggers 1 43
Java 8 to Java 6 8 20
Running JavaFX on the Raspberry Pi 27 41
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

837 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