Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP not showing images whose physical path is fetched from mysql database

 
Imdad Soomro
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,
I am developing a book store web application where admin uploads the books and those books are displayed on the JSP Page. Admin inserts name, author and physical path of book image. All are stored in db and image is also saved on the location given in physical path. Image is uploaded using Apache commons library.
Problem occurs when records are displayed but image does not although image is physical present in the location. I have used several ways to display the image but none has worked. Although hard coded path displays the image. Following are few ways i have used in JSP file:

<%
ArrayList l = (ArrayList)request.getAttribute("books");
b = (Book)l.get(0);
String path = "C:\\Users\\tom\\workspace\\BooksLibrary\\WebContent\\"+b.getFilePath(); // b.getFilePath() returns "images/err.jpg"
%>
<img src=<%=path %> alt="image" width="100" height="150" />

I have also tried:
<img src="http:\\localhost:8080\BooksLibrary\<%out.println(b.getFilePath());%>" alt="image" width="100" height="150" />

I am using eclipse and images are saved at :
C:\Users\tom\workspace\BooksLibrary\WebContent\images\

Kind response will be appreciated.
Thanks
 
Albareto McKenzie
Ranch Hand
Posts: 299
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will never, ever be able to access an image that is stored in a server using a path like c:\\..., you must understand that c:\\ is a local path related to your server and you are trying to access the image from a client that is not in the same machine. Let alone the security problems that has the fact that someone would be able just to access files directly in the server using a simple path...

You have to deploy the images in your server, just like your JSPs an access them the same way. For example if you have them under WebappRoot/web/images, then you can access them just as if they were a js file, a css or whatever.

If you have them in your server maybe you are just forgetting the context path. Other thing, that you have them in your workspace does not automatically mean that they are deployed in your server.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sabarish Jaganathan, Your post was moved to a new topic. Please start your own topic for your own questions.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic