A JSP page is compiled into a Servlet; therefore, it can do everything that a Servlet can do. If the above statement is TRUE, then why is an IllegalStateException thrown when opening a Binary Stream output to the client from a JSP page but NOT to a Servlet? Choice 1,JSP pages can write a Binary Stream using its implicit out stream. Choice 2,JSP has already opened the stream as a JspWriter. Choice 3,JSP pages must flush their buffers before changing the stream type; Servlets do not have this limitation.
You can get some of the information you want by looking at the source code created when compiling a JSP into a servlet, and from looking at the ways BinaryStreams and JspWriters are obtained from the request object. I have more to say, but lets take baby-steps for now. Dave
posted 16 years ago
I think the exception occurs because Stream is already open.out implicit object is open during jsp translation & may be due to this it throws IllegalStateException if we try to write. What du u say, David?
Welcome to the Ranch Sidhu C! You'll find this forum a great place to seek help on JSP pages, and there aren't many rules you'll have to worry about, but one is that proper names are required. Please take a look at the JavaRanch Naming Policy and change your display name to match it. Thanks! bear JSP Forum Bartender
OK, I have a little bit of time so I'll drop some more in the hope you're about to change your display name and are still reading. The implicit 'out' object is a JsPWriter and you cannot write binary data to it. This rules out one of your options. You cannot get a JspWriter then an OutputStream or vice versa, this will always cause an IllegalStateException. It's a bit hard to flush a writer if you're not allowed to have one. This also rules out one of your options. If the writer is already opened, you wouldn't be able to write binary data in a JSP without causing a IllegalStateException. Since it is possible for JSPs to output binary data, this also rules out one of your options. To output binary data in a JSP, there are some tricks and pitfalls to be aware of. The hard rule is: If you load the BinaryStream, anything that causes the writer to be called will cause an exception. This is what we have to be careful of. Firstly, any white-space in your JSP will cause the JspWRiter to be called and will kill you application. White space can look like this:
The carriage return outside the scriptlets between the first and second blocks is actually HTML data and gets sent via the JspWriter. You need to do this:
I was using an editor once that hid a trailing carriage return and this caused my JSP to keep crashing. Be aware of this, it's very annoying. The other thing to be aware of is that any exceptions that occur will cuase the container to forward to the error handling page. When it sends the error page, it will try to send it using the JspWriter. You guessed it, this causes an exception. You can either wrap your own try/catch block, or just live with this behaviour - you should try to write the JSP so that it doesn't throw any exceptions anyway. Hopefully this answered your question and possibly the next one too. Dave