1 Reply Latest reply: Feb 27, 2012 9:55 PM by Steve Button-Oracle RSS

    IllegalStateException: strict servlet API: cannot call getWriter()

      I am getting an exception in Weblogic when I am trying to display a pdf file in a web page.

      java.lang.IllegalStateException: strict servlet API: cannot call getWriter() after getOutputStream()
      I am using the following code code
      ByteArrayOutputStream baos;
      ServletOutputStream out = response.getOutputStream();

      I have seen this error in so many discussion forum and there is no proper solution for this error.

      I am able to see the pdf page even with this exception. But I want to get rid of this exception also from Weblogic.
        • 1. Re: IllegalStateException: strict servlet API: cannot call getWriter()
          Steve Button-Oracle
          I just did a quick (and dirty!) test with the following to display a jpg image and it worked for me without any trace of an exception:
              protected void doGet(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
                  File f = new File("/Users/me/Desktop/IMG_0032.JPG");
                  byte[] bits = new byte[(int) f.length()];
                  FileInputStream fos = new FileInputStream(f);
                  ByteArrayOutputStream baos = new ByteArrayOutputStream();
                  baos.write(bits, 0, bits.length);
          Note that there is no call anywhere to response.getWriter() in that.

          I'd guess the exception is directing you at what the problem is, at least as the server is seeing it -- somewhere in the execution of the request path there would appear to be call to response.getWriter() occurring after a call to response.getOutputStream() has been called, which is not permitted on WLS according to the error message.

          Do you use filters on this app? If response.getWriter() has been called on the same response object in a post request filter phase, that would be on the same call path with the same response object and would result in the exception.