xiaoyunwu
asked on
getCompressingOutputStream() has already been called
I'm using Broadvision to write code, it's using Struts framework.
I have the following code to create pdf using iText.
HttpServletResponse response = (HttpServletResponse) this.getInputs()
.get("javax.servlet.http.H ttpServlet Response") ;
response.setContentType("a pplication /pdf");
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(docu ment, response
.getOutputStream());
In my web.xml, I have the following filter:
<filter>
<!-- File compression -->
<filter-name>CompressingFi lter</filt er-name>
<filter-class>com.planetj. servlet.fi lter.compr ession.Com pressingFi lter</filt er-class>
</filter>
<filter-mapping>
<filter-name>CompressingFi lter</filt er-name>
<url-pattern>/*</url-patte rn>
</filter-mapping>
When I call the program to generate pdf, I got the following error in the log file. Even though the pdf generated correctly. How can I get rid of the log?
2011-10-18 13:39:50,142 ERROR [org.apache.catalina.core. ContainerB ase.[jboss .web].[loc alhost].[/ tec].[jsp] ] Servlet.service() for servlet jsp threw exception
java.lang.IllegalStateExce ption: getCompressingOutputStream () has already been called
at com.planetj.servlet.filter .compressi on.Compres singHttpSe rvletRespo nse.getWri ter(Compre ssingHttpS ervletResp onse.java: 96)
at javax.servlet.ServletRespo nseWrapper .getWriter (ServletRe sponseWrap per.java:1 12)
I think it's casued by:
http://grepcode.com/file/repo1.maven.org/maven2/net.sourceforge.pjl-comp-filter/pjl-comp-filter/1.6.4/com/planetj/servlet/filter/compression/CompressingHttpServletResponse.java
public PrintWriter [More ...] getWriter() throws IOException {
95
if (isGetOutputStreamCalled) {
96
throw new IllegalStateException("get Compressin gOutputStr eam() has already been called");
97
}
98
isGetWriterCalled = true;
99
if (printWriter == null) {
100
printWriter = new PrintWriter(new OutputStreamWriter(getComp ressingSer vletOutput Stream(),
101
getCharacterEncoding()),
102
true);
103
}
104
return printWriter;
105
}
I have the following code to create pdf using iText.
HttpServletResponse response = (HttpServletResponse) this.getInputs()
.get("javax.servlet.http.H
response.setContentType("a
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(docu
.getOutputStream());
In my web.xml, I have the following filter:
<filter>
<!-- File compression -->
<filter-name>CompressingFi
<filter-class>com.planetj.
</filter>
<filter-mapping>
<filter-name>CompressingFi
<url-pattern>/*</url-patte
</filter-mapping>
When I call the program to generate pdf, I got the following error in the log file. Even though the pdf generated correctly. How can I get rid of the log?
2011-10-18 13:39:50,142 ERROR [org.apache.catalina.core.
java.lang.IllegalStateExce
at com.planetj.servlet.filter
at javax.servlet.ServletRespo
I think it's casued by:
http://grepcode.com/file/repo1.maven.org/maven2/net.sourceforge.pjl-comp-filter/pjl-comp-filter/1.6.4/com/planetj/servlet/filter/compression/CompressingHttpServletResponse.java
public PrintWriter [More ...] getWriter() throws IOException {
95
if (isGetOutputStreamCalled) {
96
throw new IllegalStateException("get
97
}
98
isGetWriterCalled = true;
99
if (printWriter == null) {
100
printWriter = new PrintWriter(new OutputStreamWriter(getComp
101
getCharacterEncoding()),
102
true);
103
}
104
return printWriter;
105
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I'm not sure I understand this:
>No. I think not. It's working fine while I call response.getOutputStream() .
>And if I change response.getOutputStream() to write the file on server, the error disappears
If you just make it itn two steps:
OutputStream os = response.getOutputStream() ;
and then
PdfWriter writer = PdfWriter.getInstance(docu ment, out);
on which step it would produce the error?
probably on the first step ?
>No. I think not. It's working fine while I call response.getOutputStream()
>And if I change response.getOutputStream()
If you just make it itn two steps:
OutputStream os = response.getOutputStream()
and then
PdfWriter writer = PdfWriter.getInstance(docu
on which step it would produce the error?
probably on the first step ?
ASKER
OutputStream os = response.getOutputStream()
PdfWriter writer = PdfWriter.getInstance(docu
on which step it would produce the error?
probably on the first step ?
>> None of the steps produce error. The error generated from the filter.
com.planetj.servlet.filter
And the filter really makes it all working faster?
Perhpas this remark below from here
http://download.oracle.com/docs/cd/E13222_01/wls/docs81/jsp/reference.html
may have some connection with this sitaution, as I guess one channel to communicate with
the reponse.out stream was already opened implicitly
-------------------------- --
out is an instance of javax.jsp.JspWriter that has several methods you can use to send output back to the browser.
If you are using a method that requires an output stream, then JspWriter does not work. You can work around this limitation by supplying a buffered stream and then writing this stream to out. For example, the following code shows how to write an exception stack trace to out:
ByteArrayOutputStream ostr = new ByteArrayOutputStream();
exception.printStackTrace( new PrintWriter(ostr));
out.print(ostr);
------------------------
if you use some byte buffer in this command
PdfWriter writer = PdfWriter.getInstance(docu ment, response
.getOutputStream());
instead of "response.getOutputStream( );" as shown above
and then use something similar to
out.print() to ultimately send your PDF
maybe that will not generate the error
I think in this example they disconnect these two steps:
http://www.mail-archive.com/itext-questions@lists.sourceforge.net/msg09964.html
Don't know iif you want to go through all pains and
experiments just because of the warning in the log.
http://download.oracle.com/docs/cd/E13222_01/wls/docs81/jsp/reference.html
may have some connection with this sitaution, as I guess one channel to communicate with
the reponse.out stream was already opened implicitly
--------------------------
out is an instance of javax.jsp.JspWriter that has several methods you can use to send output back to the browser.
If you are using a method that requires an output stream, then JspWriter does not work. You can work around this limitation by supplying a buffered stream and then writing this stream to out. For example, the following code shows how to write an exception stack trace to out:
ByteArrayOutputStream ostr = new ByteArrayOutputStream();
exception.printStackTrace(
out.print(ostr);
------------------------
if you use some byte buffer in this command
PdfWriter writer = PdfWriter.getInstance(docu
.getOutputStream());
instead of "response.getOutputStream(
and then use something similar to
out.print() to ultimately send your PDF
maybe that will not generate the error
I think in this example they disconnect these two steps:
http://www.mail-archive.com/itext-questions@lists.sourceforge.net/msg09964.html
Don't know iif you want to go through all pains and
experiments just because of the warning in the log.
ASKER
Thank you, for_yan.
Since there's no performance penality, so I caught the exception and not throw it further.
Since there's no performance penality, so I caught the exception and not throw it further.
ASKER
Since the code is in lib/pjl-comp-filter-1.6.ja
>> So you say, it still returns you the writer - maybe you happen to call this method getWriter() twice - it first returns the writer,
and copmplains second time ?
No. I think not. It's working fine while I call response.getOutputStream()
And if I change response.getOutputStream()
Here is the quote from the following link:
http://stackoverflow.com/questions/1776142/getoutputstream-has-already-been-called-for-this-response
The issue here is that your JSP is talking directly to the response OutputStream. This technically isn't forbidden, but it's very much not a good idea.
Specifically, you call response.getOutputStream()
You should be writing this code as a Servlet, not a JSP. JSPs are only really suitable for textual output as contained in the JSP. You can see that there's no actual text output in your JSP, it only contains java.