java - Display image on jsp page through Servlet -
this question has answer here:
i making small application uploads image authenticated user database , displays images uploaded dynamically through servlet.
i retrieve images @ page load database using code:
<head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <jsp:include page="/retrieveimagesservlet.do"></jsp:include> </head>
this sets user images in page request , working fine.
now display image data in table format use jstl. since user image objects contains image in byte[]
format invoke servlet print image file 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 servlet display contains 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 able image in invoked servlet able print imagename throws illegalstateexception @ final outputstream outputstream=response.getoutputstream();
.
please guide me wrong , how can display image on jsp file.
you don't understand how browsers , http work. when load html page, make http request. response request page containing html code. example:
<img src="someimage.png" />
the browser parses html, sees there img
tag, , sends second, different request someimage.png
. , response second request bytes of image.
you're trying send, in response first request, html code and bytes of image. can't work. html page generated jsp must include img
tag, pointing @ servlet send bytes of image response. example:
<img src="imagedisplayservlet.do?imageid=${imagedto.id}" />
Comments
Post a Comment