tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Lyallex <>
Subject Images and response.getOutputStream()
Date Tue, 03 Jul 2007 09:36:09 GMT

I'm not sure this is a Tomcat question, more likely a poor understanding of
the Request/Response lifecycle on my part but I'll give it a go anyway.

After agonising for days(weeks) over whether to store my images in a
database or on the filesystem I opted for the database.

I have everything working fine ... apart from one little niggling thing that
I just cannot figure out.

Every time I get an image from the database I get an exception

java.lang.IllegalStateException: getOutputStream() has already been called
for this response ...

Now I think I understand why this is happening, I read somewhere that you
cannot get a JspWriter (via a call to the implicit object out for example)
and a binary output stream
(ServletOutputStream outStream = response.getOutputStream ()) from the same
response. Nothing falls over and the images appear fine. It's really the
exception traces I'm trying to get rid of

It appears that others are also using a database to store images and have
things working.
(mainly from

Can anyone advise me as to how to write both text and image data out to the
same response without this exception clogging up all my log files ?

I have included code below if you want/need to read it

Many thanks

//============== get an image in a jsp =============
//where p is a Product Object that contains information on the image name as
stored in the database
//images can be thumbnails or normal size

<img src="imageproxy.jsp?imageid=<%=p.calculateImageId()%>&imagetype=thumb"

//============== imageproxy.jsp ================
//where ImageServer is a singleton that gets an InputStream on a MySQL
database BLOB

        boolean DEBUG = false;

        ServletOutputStream outStream = response.getOutputStream();

        response.setHeader("Expires", "-1");
        response.setHeader("Cache-Control", "no-cache");

        String imageId = request.getParameter (imageid);
        String imageType = request.getParameter(imagetype);

        ImageServer images = ImageServer.getServer();
        InputStream inStream = images.getImageData(imageId, imageType);

            System.out.println("imageproxy.jsp, inStream has " +
inStream.available() + " bytes available");

        byte[] bytes = new byte[1000];
        int bytesRead =;
        while (bytesRead != -1) {
            bytesRead =;
        outStream.flush ();

//============= Just FYI here is the trace (one per image) =============

SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.IllegalStateException: getOutputStream() has already been called
for this response
        at org.apache.catalina.connector.Response.getWriter(
        at org.apache.catalina.connector.ResponseFacade.getWriter(
        at org.apache.jasper.runtime.JspWriterImpl.initOut (
        at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(
        at org.apache.jasper.runtime.PageContextImpl.release(
org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext (
        at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(
        at org.apache.jsp.imageproxy_jsp._jspService(
        at org.apache.jasper.runtime.HttpJspBase.service (
        at javax.servlet.http.HttpServlet.service(
        at org.apache.jasper.servlet.JspServletWrapper.service(
        at org.apache.jasper.servlet.JspServlet.serviceJspFile (
        at org.apache.jasper.servlet.JspServlet.service(
        at javax.servlet.http.HttpServlet.service(
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(
        at org.apache.catalina.core.StandardWrapperValve.invoke( :213)
        at org.apache.catalina.core.StandardContextValve.invoke(
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(
        at org.apache.catalina.core.StandardHostValve.invoke (
        at org.apache.catalina.valves.ErrorReportValve.invoke(
        at org.apache.catalina.core.StandardEngineValve.invoke(
        at org.apache.catalina.connector.CoyoteAdapter.service(
        at org.apache.coyote.http11.Http11Processor.process(
        at :80)
        at org.apache.tomcat.util.threads.ThreadPool$

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message