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.zip.*,java.io.*,java.util.*"%> <%<br /> Character qCh = new Character('"');<br /> String qStr = new String(qCh.toString());<br /> String encodings = request.getHeader("Accept-Encoding"); <br /> PrintWriter outWriter = null; <br /> if ((encodings != null) && (encodings.indexOf("gzip") != -1)) { <br /> OutputStream outA = response.getOutputStream(); <br /> outWriter = new PrintWriter(new GZIPOutputStream(outA), false); <br /> response.setHeader("Content-Encoding", "gzip"); <br /> // System.out.println("ZIPPED VERSION"); <br /> } else { <br /> //System.out.println("UN-ZIPPED VERSION"); <br /> outWriter = new PrintWriter(response.getOutputStream(), false); <br /> }<br /> outWriter.println("Foo Bar"); <br /> outWriter.println("TESTTTTTTTING");<br /> outWriter.println("< !--End of Custom Footer -->"); <br /> outWriter.close();<br /> %>
Kyle Brown
author
Ranch Hand
Joined: Aug 10, 2001
Posts: 3878
posted
0
Originally posted by Matthew Brown: 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.zip.*,java.io.*,java.util.*"%> < %<br /> Character qCh = new Character('"');<br /> String qStr = new String(qCh.toString());<br /> String encodings = request.getHeader("Accept-Encoding"); <br /> PrintWriter outWriter = null; <br /> if ((encodings != null) && (encodings.indexOf("gzip") != -1)) { <br /> OutputStream outA = response.getOutputStream(); <br /> outWriter = new PrintWriter(new GZIPOutputStream(outA), false); <br /> response.setHeader("Content-Encoding", "gzip"); <br /> // System.out.println("ZIPPED VERSION"); <br /> } else { <br /> //System.out.println("UN-ZIPPED VERSION"); <br /> outWriter = new PrintWriter(response.getOutputStream(), false); <br /> }<br /> outWriter.println("Foo Bar"); <br /> outWriter.println("TESTTTTTTTING");<br /> outWriter.println("< !--End of Custom Footer -->"); <br /> outWriter.close();<br /> %>
What this error is telling you is that you can't really do this. You're not actually changing the output writer -- that's not allowed. The standard output writer is already created and held in the variable "out". What you're doing is creating a second output writer on the same output stream -- this the exception. If you use the -keepcompiled flag and take a look at the generated Java code for your JSP you'll see what I mean. Kyle ------------------ Kyle Brown, Author of Enterprise Java (tm) Programming with IBM Websphere See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information. [This message has been edited by Kyle Brown (edited November 26, 2001).]
Kyle- the only thing I don't understand is why the exception doesn't stop the web page form being displayed, and why it allows "normal" processing of the rendered jsp- even though the app server is throwing a fit. I guess I'm wondering if I can get away with it?On the other hand, I guess I could easily convert it to a servlet and not have to worry about it anymore.
subject: JSP in websphere throwing illegal state exception