I'm new to jsp and java and I'm currently trying to display an image on a jsp page from a folder that is located in my C: drive..but for some reason I'm not getting it to show up no matter what I try...I currently have it as shown below:
<img src="/argsdbPics/mazda.jpg" width = "90" height = "55" alt ="mazda"/>
What is the cause of this and how can it be dealt with?
It has to be available by a http://
URL (like as you'd usually enter in browser's address bar). It's namely the webbrowser who has got to download the images individually by their URLs, not the webserver who has got to auto-include them during generating the JSP somehow.
Easiest way would be to edit Tomcat's /conf/server.xml
and add a new <Context>
element to register a virtual URL on an arbitrary folder. Assuming that the image folder is located in C:\path\to\argsdbPics
, here's an example:
<Context docBase="/path/to/argsdbPics" path="/argsdbPics" />
This way the folder contents is available by http://localhost:8080/argsdbPics/...
. So if you have a mazda.jpg
in the folder C:\path\to\argsdbPics
, this way it'll be available by http://localhost:8080/argsdbPics/mazda.jpg
. This way your following HTML attempt should work:
<img src="/argsdbPics/mazda.jpg" />
I found a great way if you are using struts and jsp, without any tomcat changes you can implements the ServletRequestAware interface and call it thus way:
String filePath = servletRequest.getRealPath("/");
I did a tricky solution.. I know it's not the best but it's working..
When I upload the photos to the directory I crypt them with a random Cypher key and I gave them a random name.. The key and the name are stored in the database..
So when I want to display a photo I decrypt it in a temporary folder and I read it.. And i deleted when the needed page is no more used..
Like that even when you got the real path you can't use the crypted file.