Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

OutputStream already obtained error while generating a pdf

 
nitin singh
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 502
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems like you are trying to obtain Response OutputStream more than a time. which is not allowed.
 
nitin singh
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 502
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you post whole JSP file?, if it is small or you can send me through email.
 
nitin singh
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<%@ 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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<%@ 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
Posts: 502
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In struts-config.xml, did you specify redirect="true" in <forward ...> element for that particular action class?
 
nitin singh
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes its there.. what diff it will make ?
 
Prabhu Venkatachalam
Ranch Hand
Posts: 502
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 502
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is code snippet.

In your action class,



And retun null; which will show you PDF in browser.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
nitin singh
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Surendra Jadon
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

In WCS7, I am getting the error below while trying to take printout (pdf file) by clicking on button on one jsp.

[12/26/12 12:51:21:873 EST] 00000036 servlet E com.ibm.ws.webcontainer.servlet.ServletWrapper service SRVE0068E: Uncaught exception created in one of the service methods of the servlet /error.jsp in application WC_giftreg. Exception created : java.lang.IllegalStateException: SRVE0199E: OutputStream already obtained
at com.ibm.ws.webcontainer.srt.SRTServletResponse.getWriter(SRTServletResponse.java:696)
at com.ibm.ws.cache.servlet.FragmentComposer.obtainWriter(FragmentComposer.java:704)
at com.ibm.ws.cache.servlet.FragmentComposer.getPrintWriter(FragmentComposer.java:837

Scenario is by clicking on button of one jsp another jsp window opens that shows the content to print but the second jsp (popup jsp) is getting blank and processing for long time and no printout comes.

This issue is coming at production site.

Please help on this.

Thanks in advance.

Surendra Kumar
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic