Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Apache FOP with Struts

Posted on 2004-08-19
4
Medium Priority
?
1,176 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 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
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.
Suggested Courses

618 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