You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Eduardo Pelegri--Llopart <Ed...@eng.sun.com> on 2000/05/01 20:04:07 UTC

Re: HELP: Use of OutputStream from JSP causes IllegalStateException

This topic comes up now and then.  I'll look into the issues in JSP 1.2,
both from the perspective of clarifying the spec and also on the exact
role of the JspWriter and how to generate things like binary data.  Two
arguments on revisiting this are: tag libraries, and using a JSP to
customize a request before forwarding it to a Servlet.

thanks,
	- eduard/o


Steven Punte wrote:
> 
> Ed:
> 
>     Thanks for the explanation.
>     I understand the situation now.
> 
>     I tried closing the JspWriter object, and then
>     re-opening it when I was done with the binary
>     stream write, but still gave problems.
> 
>     I guess I'll move this task into a servlet.
> 
>     Funny, until now, I had been thinking that a JSP
>     page could do just about anything a servlet
>     could do.  But this proves this concept
>     wrong.
> 
>     Sincerely
> 
>         STeve
> 
> ----- Original Message -----
> From: Tice, Ed <ET...@bluestone.com>
> To: 'Steven Punte ' <st...@hamsoft.com>
> Cc: <to...@jakarta.apache.org>
> Sent: Thursday, April 27, 2000 5:49 PM
> Subject: RE: HELP: Use of OutputStream from JSP causes IllegalStateException
> 
> > Steven,
> >
> >   This is correct.  The specification says that since a JspWriter has
> > already been obtained (and assigned to the implicit variable out) that you
> > can not go back and get the output stream this way.
> >
> >   If you need to get this output stream (most likely because you want to
> > write binary data), you need to use a regular servlet, not a JSP.
> >
> >   This is in the servlet spec, but you may not see the violation, because
> > you didn't write the code to get the JSPWriter, but it is present as part
> of
> > the automatically generated servlet template.
> >
> >   The template contains
> >
> >         JspWriter out = null;
> > ...
> >             out = pageContext.getOut();
> >
> >   Now that this call has been made, it is not legal to go back and call
> > response.getOutputStream().  A servlet is faced with making the choice of
> > one or the other.  Implicit in writing a JSP is your choice to use the
> > JspWriter.
> >
> > Ed
> >
> > -----Original Message-----
> > From: Steven Punte
> > To: tomcat-user@jakarta.apache.org
> > Sent: 4/27/00 8:38 PM
> > Subject: HELP: Use of OutputStream from JSP causes IllegalStateException
> >
> > In order to output a binary image from a JSP file,
> > I'm using method calls on the ServletOutputStream
> > object obtained from response.getOutputStream().
> >
> > Although it seems my JSP is operational, I keep
> > getting an exception in ~/logs/tomcat.log of basically:
> >
> >         at java.lang.IllegalStateException.<init>(Compiled Code)
> >         at org.apache.tomcat.core.ResponseImpl.getWriter(Compiled Code)
> >         at
> > org.apache.tomcat.core.HttpServletResponseFacade.getWriter(Compiled
> > Code)
> >         at org.apache.jasper.runtime.JspWriterImpl.initOut(Compiled
> > Code)
> >         at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(Compiled
> > Code)
> >         at org.apache.jasper.runtime.JspWriterImpl.flush(Compiled Code)
> >         at
> > pages._0002fpages_0002fimage_0002ejspimage_jsp_8._jspService(Compiled
> > Code)
> >                 <The above line is my JSP page>
> >
> > where the error message is "OutputStream is already being used for this
> > request".
> >
> > I have tried using outputStream.close() and outputStream.flush()
> > to try to get rid of this problem, but with  no success.
> >
> > At digging into this code, and looking at the above, it seems like a
> > defect
> > in JspWriteImpl, in that it is trying to get another Writer on the
> > outputsteam
> > before the existing one is finished.
> >
> > I've seen a lengthy earlier post that "seems" to be a similar problem,
> > but fundamentally was a overall system architecture issue.
> >
> > Any suggestions here?
> >
> >
> >
> >
> > STeve Punte
> > e-Business Software Architect
> > Technologent Inc
> > stevep@technologent.com
> >
> >
> > ------------------------------------------------------------------------
> > --
> > To unsubscribe, email: tomcat-user-unsubscribe@jakarta.apache.org
> > For additional commmands, email: tomcat-user-help@jakarta.apache.org
> >
> > --------------------------------------------------------------------------
> > To unsubscribe, email: tomcat-user-unsubscribe@jakarta.apache.org
> > For additional commmands, email: tomcat-user-help@jakarta.apache.org
> 
> --------------------------------------------------------------------------
> To unsubscribe, email: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commmands, email: tomcat-user-help@jakarta.apache.org