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.HttpServletResponse");

            response.setContentType("application/pdf");
            Document document = new Document();
            PdfWriter writer = PdfWriter.getInstance(document, response
                        .getOutputStream());


In my web.xml, I have the following filter:
  <filter>
  <!-- File compression -->
  <filter-name>CompressingFilter</filter-name>
  <filter-class>com.planetj.servlet.filter.compression.CompressingFilter</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>CompressingFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </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.ContainerBase.[jboss.web].[localhost].[/tec].[jsp]] Servlet.service() for servlet jsp threw exception
java.lang.IllegalStateException: getCompressingOutputStream() has already been called
        at com.planetj.servlet.filter.compression.CompressingHttpServletResponse.getWriter(CompressingHttpServletResponse.java:96)
        at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:112)


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("getCompressingOutputStream() has already been called");

97

             }

98

             isGetWriterCalled = true;

99

             if (printWriter == null) {

100

                  printWriter = new PrintWriter(new OutputStreamWriter(getCompressingServletOutputStream(),

101

                                                                       getCharacterEncoding()),

102

                                                true);

103

            }

104

            return printWriter;

105

      }
xiaoyunwuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:
If you just want to get rid of log record - you can catch this exception and not throw it further abut rather rwite something less
dramatic

If you want to get to the root cause of the problem, that's another story

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 ?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
xiaoyunwuAuthor Commented:
>>If you just want to get rid of log record - you can catch this exception and not throw it further abut rather rwite something less dramatic
Since the code is in lib/pjl-comp-filter-1.6.jar, and I didn't write it. I guess I need to learn how to get it compiled and put it back. Before I do that, I need to know if it will have negative effect.


>> 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() to write the file on server, the error disappears.

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() and write data to that. Later, when the JSP engine tries to flush the response, it fails because your code has already "claimed" the response. An application can either call getOutputStream or getWriter on any given response, it's not allowed to do both. JSP engines use getWriter, and so you cannot call 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.
0
for_yanCommented:
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(document, out);

on which step it would produce the error?

probably  on the first step ?


0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

xiaoyunwuAuthor Commented:

OutputStream os = response.getOutputStream();

PdfWriter writer = PdfWriter.getInstance(document, out);

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.compression.CompressingFilter
0
for_yanCommented:
And the filter really makes it all working faster?
0
for_yanCommented:
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(document, 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.



0
xiaoyunwuAuthor Commented:
Thank you, for_yan.

Since there's no performance penality, so I caught the exception and not throw it further.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

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.