java - How to retrieve and display images from a database in a JSP page? -
how can retrieve , display images database in jsp page?
let's see in steps should happen:
- jsp view technology supposed generate html output.
- to display image in html, need html
<img>
element. - to let locate image, need specify
src
attribute. - the
src
attribute needs point validhttp://
url , not local disk file system pathfile://
never work when server , client run @ physically different machines. - the image url needs have image identifier in either request path (e.g.
http://example.com/context/images/foo.png
) or request parameter (e.g.http://example.com/context/images?id=1
). - in jsp/servlet world, can let servlet listen on url pattern
/images/*
, can execute java code on specific url's. - images binary data , obtained either
byte[]
orinputstream
db, jdbc api offersresultset#getbytes()
,resultset#getbinarystream()
this, , jpa api offers@lob
this. - in servlet can write
byte[]
orinputstream
outputstream
of response usual java io way. - the client side needs instructed data should handled image, @ least
content-type
response header needs set well. can obtain right 1 viaservletcontext#getmimetype()
based on image file extension can extend and/or override via<mime-mapping>
inweb.xml
.
that should it. writes code itself. let's start html (in jsp):
<img src="${pagecontext.request.contextpath}/images/foo.png"> <img src="${pagecontext.request.contextpath}/images/bar.png"> <img src="${pagecontext.request.contextpath}/images/baz.png">
you can if necessary dynamically set src
el while iterating using jstl:
<c:foreach items="${imagenames}" var="imagename"> <img src="${pagecontext.request.contextpath}/images/${imagename}"> </c:foreach>
then define/create servlet listens on requests on url pattern of /images/*
, below example uses plain vanilla jdbc job:
@webservlet("/images/*") public class imageservlet extends httpservlet { // content=blob, name=varchar(255) unique. private static final string sql_find = "select content image name = ?"; @resource(name="jdbc/yourdb") // tomcat, define <resource> in context.xml , declare <resource-ref> in web.xml. private datasource datasource; @override protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { string imagename = request.getpathinfo().substring(1); // returns "foo.png". try (connection connection = datasource.getconnection(); preparedstatement statement = connection.preparestatement(sql_find)) { statement.setstring(1, imagename); try (resultset resultset = statement.executequery()) { if (resultset.next()) { byte[] content = resultset.getbytes("content"); response.setcontenttype(getservletcontext().getmimetype(imagename)); response.setcontentlength(content.length); response.getoutputstream().write(content); } else { response.senderror(httpservletresponse.sc_not_found); // 404. } } } catch (sqlexception e) { throw new servletexception("something failed @ sql/db level.", e); } } }
that's it. in case worry head , caching headers , responding on requests, use abstract template static resource servlet.
Comments
Post a Comment