0

I am making a small application which uploads a image for authenticated user into database and displays all the images that were previously uploaded dynamically through servlet.

I retrieve images at page load from database using this code:

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<jsp:include page="/RetrieveImagesServlet.do"></jsp:include>
</head>

This sets the user images in the page request and is working fine.

Now to display image data in table format i use jstl. Since user image objects contains image in byte[] format so I invoke a servlet to print the image file as below.

<c:forEach var="imageDto" items="${requestScope.userImages}">
                <tr>
                    <td><c:out value="${imageDto.getSerialNumber()}"></c:out></td>
                    <td><c:out value="${imageDto.getImageName() }"></c:out></td>
                    <td><c:out value="${imageDto.getImageSize()}"></c:out></td>
                    <td>
                        <c:set var="imageDto" scope="request" value="${imageDto}"></c:set>
                        <jsp:include page="/ImageDisplayServlet.do"></jsp:include> 
                        <c:remove var="imageDto" scope="request" /></td>
                    <td></td>
                </tr>
            </c:forEach>

The line <jsp:include page="/ImageDisplayServlet.do"></jsp:include> included in above code invokes a servlet to display which contains the code below

response.setContentType("image/jpeg");
    final ImageDto imageDto = (ImageDto) request.getAttribute("imageDto");
    System.out.println(imageDto.getImageName());
    final OutputStream outputStream=response.getOutputStream();
    outputStream.write(imageDto.getImageFile());
    outputStream.close();

I am able to get the image in invoked servlet as it is able to print the imageName but it throws illegalStateException at final OutputStream outputStream=response.getOutputStream();.

Please guide me where I am wrong and how can I display image on jsp file.

Pravesh Gupta
  • 13
  • 2
  • 6

1 Answers1

0

You don't understand how browsers and HTTP work. When you load an HTML page, you make a HTTP request. The response to the request is a page containing HTML code. For example:

<img src="someImage.png" />

The browser then parses the HTML, sees that there is an img tag, and then sends a second, different request to someImage.png. And the response to this second request is the bytes of the image.

You're trying to send, in the response to the first request, the HTML code and the bytes of the image. That can't work. The HTML page generated by the JSP must include an img tag, pointing at the servlet which will send the bytes of the image to the response. For example:

<img src="ImageDisplayServlet.do?imageId=${imageDto.id}" />
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Yes now I understand that and finally I am doing it just like you told. But I wanted that if I have gone to my database for images first time it should return all images data and I don't have to go back for each imageId to get the image from the database. Is there any way or I have to hit the database for each imageId to get the image when I have already once went to get all images. – Pravesh Gupta Apr 13 '14 at 15:56
  • Your first query shouldn't load all the image bytes, since having the bytes at this moment is useless (unless you keep them in a cache, waiting for the next requests to come, but that will make your code stateful, and you won't know when to clear the cache). – JB Nizet Apr 13 '14 at 16:13