Solved

Apache FOP with Struts

Posted on 2004-08-19
4
1,161 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet filter example 37 84
login form jsp example 2 50
Code Coverage and Coding Standards 2 49
Java pass by reference 3 21
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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…

733 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