Apache FOP with Struts

Posted on 2004-08-19
Medium Priority
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:

Driver driver = new Driver(new org.xml.sax.InputSource("foo.fo"), response.getOutputStream());

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

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.

Question by:blackfrancis75
  • 2
LVL 35

Accepted Solution

TimYates earned 500 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
LVL 36

Expert Comment

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();
java.io.OutputStream os = response.getOutputStream();

and then we return null for the ModelAndView.

return null;

It should be more or less the same for struts.

Author Comment

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?

Author Comment

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

Thanks for the pointer Tim.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

624 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