Hello, I am working on linux environment and using Netbeans5.5.I put all the Images outside the Application,While i use <img src="/home/MyHomeGallery/image1.jpg"> its not getting the images. Can you plse suggest me ?
Chandra Bhatt
Ranch Hand
Joined: Feb 28, 2007
Posts: 1707
posted
0
I did following steps to achieve this:
1- Create a xml file under <jakarta-tomcat>\conf\Catalina\localhost mine is resourceContext.xml Content of this file is:
2- under G:/images create WEB-INF directory
3- Restart the server
Now you can access a image from your JSP page like: <img src ="/resourceContext/image1.jpg"/>
Does this work for you?
cmbhatt
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35241
7
posted
0
Remember that image paths are relative to the web root. A path like "/home/MyHomeGallery/image1.jpg" will cause the image to be looked for at "http://www.my-server.com/home/MyHomeGallery/image1.jpg" - is that where the image is located?
Thank you for your reply, I stored all the images outside the application.Not in the Application.
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12268
1
posted
0
If you expect your web server to serve these images they will have to be in a directory structure the server is allowed to access - and - the URL will have to reflect the way the server finds files, NOT the way the operating system finds them.
... Try to use fmt or core jstls tags for this, instead of <img> tag. ...
If the images are not stored in a place where they would be accessible to the web, fmt and JSTL tags will not help.
If you can't move the images to a location within your application's directory structure, you will need to write a servlet that reads the files in their current location and streams them to the browser.
You'll need to change it a little as the example uses getResourceAsStream to read the file from under the WEB-INF directory. You'll want to use java.io.File to read the file if it's not stored within your application's directory structure.
This is, for the most part, what the container would do if your images were within your web app and requested directly. Take a look at the code to Tomcat's DefaultServlet to see how it handles requests for static resources.
There is a lot more to sift through but when it comes down to it, it's doing the same thing:
Newer versions or other containers might be using nio to speed things up. You also won't be able to take advantage of any caching capability unless you write your own.
Lastly, if you want to speed up performance for images that are requested frequently by the same user, you might want to add code to check the if-modified-since request header, compare that with the timestamp of the file and return the proper header instead of the entire image with each request.
Again, if you can move the images into your webapp, the container will do all of this for you. [ November 27, 2008: Message edited by: Ben Souther ]