File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Jsp retrieve Image Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Jsp retrieve Image" Watch "Jsp retrieve Image" New topic
Author

Jsp retrieve Image

KedariNadh vsilla
Greenhorn

Joined: Nov 26, 2008
Posts: 11
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
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: 42912
    
  68
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?
KedariNadh vsilla
Greenhorn

Joined: Nov 26, 2008
Posts: 11
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: 12835
    
    5
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.

Bill
Subrahmanyam Baratam
Greenhorn

Joined: Nov 24, 2008
Posts: 13
Hi,

Try to use fmt or core jstls tags for this, instead of <img> tag.

Once check the below link also, it may help you.

http://forums.sun.com/forum.jspa?forumID=427


ThankYou,
Subrahmanyam Baratam,
WebSphere PortalDeveloper.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Subrahmanyam Baratam:

... 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.

We have a similar servlet in our CodeBarn.
http://faq.javaranch.com/java/CodeBarnSimpleStream

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Subrahmanyam Baratam
Greenhorn

Joined: Nov 24, 2008
Posts: 13
Ben, if there are number of images to displayed onto the page, then reading each image as a stream and displaying it wont't affect the performance.

If the performance won't affect its better to follow your code.I hope it will lead to a performance issue. Please reply.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.

http://svn.apache.org/repos/asf/tomcat/container/tc5.5.x/catalina/src/share/org/apache/catalina/servlets/DefaultServlet.java

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 ]
Subrahmanyam Baratam
Greenhorn

Joined: Nov 24, 2008
Posts: 13
Thank you for clearing the doubt Ben.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Jsp retrieve Image