2

I've a system that runs over Tomcat 7 run-time.

Until now I'm using the ServletOutputStream as the following:

new ServletOutputStream() {
     @Override
     public void write(final int b) throws IOException {
           responseBuilder.append(Character.toChars(b));
     }
}

But now, the system run both over Tomcat 8 and 7 (depends on user), and I have to use the abstract functions isReady and setWriteListener that not exists in Tomcat 7 in-order run over Tomcat 8 (error in Tomcat 8):

https://tomcat.apache.org/tomcat-7.0-doc/servletapi/javax/servlet/ServletOutputStream.html

https://tomcat.apache.org/tomcat-8.0-doc/servletapi/javax/servlet/ServletOutputStream.html

And if I use the abstract functions (error in Tomcat 7):

new ServletOutputStream() {
            @Override
            public void write(final int b) throws IOException {
                responseBuilder.append(Character.toChars(b));
            }

            @Override
            public boolean isReady() {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public void setWriteListener(WriteListener arg0) {
                // TODO Auto-generated method stub

            }
 }

So, how can I use ServletOutputStream both Tomcat 8 and Tomcat 7?

Shay Zambrovski
  • 401
  • 5
  • 21
  • Err, don't use `isReady()`? If you're writing for Tomcat 7 *and* 8, you aren't using non-blocking writes anyway, so you have no need for this call. – user207421 Jun 10 '18 at 09:57
  • 2
    It's abstract function in Tomcat 8... – Shay Zambrovski Jun 10 '18 at 12:26
  • 2
    You are asking how to do X when you are really trying to accomplish Y. What is "Y"? Perhaps writing your own `ServletOutputStream` isn't the right approach. You might also need to write two classes and select the right one at runtime. – Christopher Schultz Jun 10 '18 at 13:44
  • I can't write two classes, while the other will compile in tuntime x, the other will not. Also, I dont wrote my own ServletOutputStream, this is javax class... – Shay Zambrovski Jun 11 '18 at 05:11
  • 1
    Christopher is hinting for the [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Please let us know what problem you're trying to solve with this approach (read the linked article). I've never seen a situation where such a custom partial implementation of any OutputStream is required. – Olaf Kock Jun 11 '18 at 12:59
  • From the code snippet you provided in the question, you are defining an anonymous type extending/implementing `ServletOutputStream`. The correct way to "use" an `ServletOutputStream` from a servlet is to get it from `HttpServletResponse` like so: `OutputStream stream = res.getOutputStream();` – ST. Kee Jun 12 '18 at 07:29
  • @OlafKock - See https://stackoverflow.com/a/23381235/744133 – YoYo Dec 04 '20 at 21:01

0 Answers0