This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I would like to store image (.jpg) files in a database and pull out image to display on jsp webpage. Browsing web suggest that storing images in a database (MS Access) is not ideal and it is better to have images in a folder and then have links to them in the database - sounds fine to me so wondering if anyone has any comments about the two approaches?
Anyway, I have fallen at the first hurdle of trying to display image given its full path on the hard disc - the image is not part of my web app i.e. not in the war file. In my JSP I am using <img> tag below which does not display the image. (I have tried variants of /, \\ and / as file separators to no avail).
A quick read about this indicates that the web application, for security, cannot access files outside its deployed folders which ties in with what I see. (I do not see the red cross within a white box but a small jpg icon, clicking on it for properties gives: name, size and Not Available for other properties)
Is there a way around this?
I supposed I can have my action class read the image file from the hard disc then copy to a folder in my deployed app but that seems cumbersome.
The URL for any web content, including images, must be relative to the root directory of your web application. Allowing URLs with general disk access would be a major security leak (anybody could read any files on your system). The only way around this is to write a servlet that services images - it would get an image request and then look that image up from elsewhere and supply the image to the web page.
Now the example for JBoss 4.2 states: Serving Static External Files in recent 4.x versions of jboss
This works for Jboss 4.2.x and probably other versions as well.
Step 1 (optional): - Copy a default web.xml from <JBOSS_HOME>/server/<config-name>/deploy/http-invoker.sar/invoker.war/WEB-INF and place it in your external directory, for example: /home/pgib/images/WEB-INF
Step 2: Edit <JBOSS_HOME>/server/<config-name>/deploy/jboss-web.deployer/server.xml and add a Context element under Host.(Step similar to standalone TC)
<Host name="localhost" ...>
<!-- ADD static images DIRECTORY -->
<!-- The rest of your Host entity -->
Step 3: Enjoy. This will enable Tomcat to serve up static content (like html, images etc) from a directory /home/pgib/images and the url will be "http://localhost:8080/images"
The docBase path example above is linux/unix but it can be Windows path, such as: