Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Jsp retrieve Image

 
KedariNadh vsilla
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Subrahmanyam Baratam
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for clearing the doubt Ben.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic