Fast (zipped) jsp throws illegal state exception.


Web tier: servlets, JSP, Web frameworks: Fast (zipped) jsp throws illegal state exception.

  1. Fast (zipped) jsp throws illegal state exception. (1 messages)

    In order to speed up download time, I need to change my jsp output printwriter so that it sends GZIP encoded(this is adapted from Marty' Halls Core Java Servlets-but he used a servlet and I'm using a jsp). The following code works just fine,it actually works in the web browser under tomcat 3.2.1 and websphere 3.5 advanced- but an exception is thrown-Illegal State Exception: OutputStream is already being used. So when I test the page it works as needed, but the ugly exception is logged. With websphere- its alot of logging. I cannot do a servlet-which would be the easiest way of getting around this problem- does anyone have an ideas as to how to make the exception either not appear or be handled better. Here is the code:
    <%@ page import= "*,*,java.util.*"%>
    Character qCh = new Character('"');
    String qStr = new String(qCh.toString());
    String encodings = request.getHeader("Accept-Encoding");
      PrintWriter outWriter = null;
      if ((encodings != null) && (encodings.indexOf("gzip") != -1)) {
      OutputStream outA = response.getOutputStream();
      outWriter = new PrintWriter(new GZIPOutputStream(outA), false);
      response.setHeader("Content-Encoding", "gzip");
      // System.out.println("ZIPPED VERSION");
      } else {
      //System.out.println("UN-ZIPPED VERSION");
    outWriter = new PrintWriter(response.getOutputStream(), false);
    outWriter.println("Foo Bar");
    outWriter.println("<!--End of Custom Footer -->");
  2. Why are you creating a PrintWriter? It is my understanding that you should use response.getWriter() if you need a PrintWriter. I think you are also only supposed to use one or the other, getWriter() or getOutputStream(), not both. Where does your Zipped file (or unzipped binary) get written out?

    Craig Wohlfeil