aspose file tools*
The moose likes JSP and the fly likes OutputStream already obtained error while generating a pdf Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "OutputStream already obtained error while generating a pdf" Watch "OutputStream already obtained error while generating a pdf" New topic
Author

OutputStream already obtained error while generating a pdf

nitin singh
Greenhorn

Joined: Nov 07, 2006
Posts: 22
Hi
My application is a struts/tiles application.I am generating a pdf in a jsp and get follwong error.The storage token and engine i get from action class.This code is in jsp.

Code is
<%
BinaryView docBinaryView =(BinaryView)documentInstance.getView(OutputFormatType.PDF);
response.setContentType("application/pdf");
response.setHeader("Content-Type", "application/pdf");
response.setDateHeader("expires", 0);
docBinaryView.getContent(response.getOutputStream());

%>


The error i get is


Could not invoke the service() method on servlet /nccb2bjsp/ncc/bo/fetchReportPdf.jsp. Exception thrown : java.lang.IllegalStateException: SRVE0199E: OutputStream already obtained

Can anyone help.
Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

It seems like you are trying to obtain Response OutputStream more than a time. which is not allowed.


Prabhu Venkatachalam<br />SCJP 1.4,SCWCD 1.4<br />prabhu.venkatachalam@gmail.com
nitin singh
Greenhorn

Joined: Nov 07, 2006
Posts: 22
hi prabhu.Same code when Is called from Other jsp works fine.But when direct this jsp from action.This error comes.The last statement throws the error.
Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

can you post whole JSP file?, if it is small or you can send me through email.
nitin singh
Greenhorn

Joined: Nov 07, 2006
Posts: 22
<%@ page import = "com.businessobjects.rebean.wi.*,

java.util.*,

java.io.*"

%>



<%



String storageToken = request.getParameter("sEntry");



//Retrieve report engine associated with this storage token.

ReportEngine webiRepEngine = (ReportEngine)session.getAttribute("ReportEngine");



//Obtain document instance.

DocumentInstance documentInstance = webiRepEngine.getDocumentFromStorageToken(storageToken);



//Obtain report in the document.

Report report = documentInstance.getReports().getItem(0);



//Set image options. Note: When viewing in HTML, any charts or other graphical

//objects are turned into images, the image handler code is responsible for retrieving

//these images and streaming them to the browser for the user. A sample image handler

//called getImage.jsp is provided with the sample.

ImageOption imageOption = documentInstance.getImageOption();

imageOption.setImageCallback("getImage.jsp");

imageOption.setImageNameHolder("image");

imageOption.setStorageTokenHolder("sEntry");



session.setAttribute("CDZ.StorageToken", documentInstance.getStorageToken());

BinaryView docBinaryView =(BinaryView)documentInstance.getView(OutputFormatType.PDF);

response.setContentType("application/pdf");

response.setHeader("Content-Type", "application/pdf");

response.setDateHeader("expires", 0);

docBinaryView.getContent(response.getOutputStream());





%>
nitin singh
Greenhorn

Joined: Nov 07, 2006
Posts: 22
<%@ page import = "com.businessobjects.rebean.wi.*,

java.util.*,

java.io.*"

%>



<%



String storageToken = request.getParameter("sEntry");



//Retrieve report engine associated with this storage token.

ReportEngine webiRepEngine = (ReportEngine)session.getAttribute("ReportEngine");



//Obtain document instance.

DocumentInstance documentInstance = webiRepEngine.getDocumentFromStorageToken(storageToken);



//Obtain report in the document.

Report report = documentInstance.getReports().getItem(0);



//Set image options. Note: When viewing in HTML, any charts or other graphical

//objects are turned into images, the image handler code is responsible for retrieving

//these images and streaming them to the browser for the user. A sample image handler

//called getImage.jsp is provided with the sample.

ImageOption imageOption = documentInstance.getImageOption();

imageOption.setImageCallback("getImage.jsp");

imageOption.setImageNameHolder("image");

imageOption.setStorageTokenHolder("sEntry");



session.setAttribute("CDZ.StorageToken", documentInstance.getStorageToken());

BinaryView docBinaryView =(BinaryView)documentInstance.getView(OutputFormatType.PDF);

response.setContentType("application/pdf");

response.setHeader("Content-Type", "application/pdf");

response.setDateHeader("expires", 0);

docBinaryView.getContent(response.getOutputStream());





%>
Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

In struts-config.xml, did you specify redirect="true" in <forward ...> element for that particular action class?
nitin singh
Greenhorn

Joined: Nov 07, 2006
Posts: 22
yes its there.. what diff it will make ?
Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

you cannot get OutPutStream in JSP because, If you look at the Java code generated for your JSP, you notice that how "out" implicit objects are created? it is by using response.getOutputStream(). So you will get this run time exception when you try to get OutputStream more than once for same response object.

Instead of JSP, you can forward it to Servlet and do the same in servlet.
nitin singh
Greenhorn

Joined: Nov 07, 2006
Posts: 22
Thanks Prabhu.how will I generate the outout in this case?I am using struts framework I ll put it in action.Then HOw to generate the pdf?
Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

Here is code snippet.

In your action class,



And retun null; which will show you PDF in browser.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

See:
http://faq.javaranch.com/view?IllegalStateException
to find out why it is a bad idea to try to stream binary data from a JSP.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
nitin singh
Greenhorn

Joined: Nov 07, 2006
Posts: 22
Thanks .My existing code worked.Only problem was it should me in an action class.And the return from that class should be null.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Since you're looking for a Struts specific solution, I'll move this thread to our Struts forum where you are more likely to find it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: OutputStream already obtained error while generating a pdf
 
Similar Threads
Exception with displaying image
OutputStream already obtained error while generating a pdf
generating xls in servlet giving error
OutputStream already obtained error
OutputStream already obtained