*
The moose likes JSP and the fly likes image does not display on JSP page w/ Tomcat server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "image does not display on JSP page w/ Tomcat server" Watch "image does not display on JSP page w/ Tomcat server" New topic
Author

image does not display on JSP page w/ Tomcat server

Nina Anderson
Ranch Hand

Joined: Jul 18, 2006
Posts: 148
Hi,

I have a web application that does the following:

1) User uploads an image.
2) Application saves this image is a local directory on the server.
ex: C:/Users/684/Profile/images/_DSC0034.jpg
3) Application stores the newly created image path in the database.
4) Application displays the image on a web page using the path from the db:
ex: C:/Users/684/Profile/images/_DSC0034.jpg

However, in the last step where the application trys to display the image, the image FAILS to display. This used to on a Websphere server. I've moved to a Tomcat server and this does not work anywmore.

Why can't Tomcat display the actual image paths. How can I get this to work again?

Here's my code:

<img src="C:/Users/684/Profile/images/_DSC0034.jpg" name="business_images" width="100" height="130"/>

PLS...HeLP!
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Nina Anderson:
PLS...HeLP!


In an effort to help everyone get the most from our forums, we've compiled a
list of tips for asking questions here. You can find the list in our
FAQ section here.
In particular please see:
UseRealWords

Abbreviations such as "u" or "ur" in
place of "you" and "you are" or "you're" confound language translation software making
it hard for our non-English speaking members to read your posts.
"plz" is not a word in the English language.

Again, welcome to JavaRanch and good luck with your question.
-Ben


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

<img src="C:/Users/684/Profile/images/_DSC0034.jpg" name="business_images" width="100" height="130"/>


You're link is pointing to a file on the local machine.
This will only work if you're using a browser on the same machine that hosts the webserver.

You'll need to either move the file to a location within your webapp or build a servlet that can stream images from locations outside of it.
Nina Anderson
Ranch Hand

Joined: Jul 18, 2006
Posts: 148
I move my image files to the server root context like you suggested and the images is still not displaying.

In my userProfile.jsp, I tried the following with no success:

1.
<img src="Users/684/Profile/images/_DSC0034.jpg" name="business_images" width="100" height="130"/>

2.
<img src="/Users/684/Profile/images/_DSC0034.jpg" name="business_images" width="100" height="130"/>

3.
<img src="../Users/684/Profile/images/_DSC0034.jpg" name="business_images" width="100" height="130"/>

4.
<img src="../../Users/684/Profile/images/_DSC0034.jpg" name="business_images" width="100" height="130"/>


Here is my project hierarchy:

What am I doing wrong??




[BSouther: Added UBB CODE tags]
[ January 16, 2008: Message edited by: Ben Souther ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

See http://faq.javaranch.com/java/ResourceUrlProblems to find out how to create proper context relative URLs.
Nina Anderson
Ranch Hand

Joined: Jul 18, 2006
Posts: 148
Oops...hierarchy did not come out right:

-> WEB-INF
- Users
- 684
- Profile
- images
- _DSC0034.jsp
- web
- Common
- header.jsp
- footer.jsp

- Images
- bkgrnd.gif
- logo.jpg

- Search
- UserProfile.jsp
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Are you saying that the Users directory is under WEB-INF?
Nina Anderson
Ranch Hand

Joined: Jul 18, 2006
Posts: 148
Yes...the "Users" folder is under WEB-INF.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60781
    
  65

That cannot work. The server will not serve anything under WEB-INF -- that's a protected region. Move all client-referenced resources (images, style sheets and script files) out of WEB-INF.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Nina Anderson
Ranch Hand

Joined: Jul 18, 2006
Posts: 148
Sorry, disregard my last comment. The "Users" folder IS outside WEB-INF folder. It is under the "WebRoot" folder.

This used to work fine. Why is it not working with Tomcat?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60781
    
  65

We still don't know what you structure is. The chart you posted isn't useful without indentation. Please go back and fix it by enclosing the folder chart in UBB code tags and adding the proper indentation.

The only reason it wouldn't work in Tomcat is if the URL is incorrect.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Take the time to post the directory structure correctly.

I added UBB Code tags to your first directory listing which made the indents show up. From that listing, it appears that the Users directory is under (inside) WEB-INF which, as Bear said, won't work.

If you need help getting an accurate print out of your directory structure see:
http://faq.javaranch.com/java/HowToCopyEnvironmentVariablesAndDirectoryStructures
Nina Anderson
Ranch Hand

Joined: Jul 18, 2006
Posts: 148
Eureeka!!!

This worked after all:

<img src="../../Users/684/Profile/images/_DSC0034.jpg" name="business_images" width="100" height="130"/>

I found that Right-Clicking on the light red 'x' on the non-displayed image and selecting on the 'properties' actually showed the path the server was trying to display. So, that helped me debug to get it to the right location.

Thanks for your help.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60781
    
  65

<img src="../../Users/

This is a bad bad idea. Your URL is page-relative and is fragile and will break not only if any structure gets moved around, but even if you address the page in a different way (as the result of a forward or include, for example).

I strongly urge you to pay attention to the posts that have been made and adopt best-practice server-relative URLs.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Bear Bibeault:
I strongly urge you to pay attention to the posts that have been made and adopt best-practice server-relative URLs.


+1

See:
http://faq.javaranch.com/java/ResourceUrlProblems
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: image does not display on JSP page w/ Tomcat server
 
Similar Threads
displaying images in jsp page..
Jsp retrieve Image
Image display problem
What is best way to name jpg images in a web application?
Accessing Remote Files