aspose file tools*
The moose likes JSP and the fly likes how to insert an image Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "how to insert an image" Watch "how to insert an image" New topic
Author

how to insert an image

Popescu Ion
Ranch Hand

Joined: Jan 14, 2011
Posts: 33
Hi,
Have a Dynamic web project named "project" and a jsp file named "index.jsp" under the WebContent folder of project. \project\WebContent\index.jsp
I am trying to insert an image using <img src = "file.png" />. file.png is locating in WebContent folder. After I run it, all the code works fine, but image doesn't load, it's look like a red "X". What is the problem, where should i put my images and how to write correct location. Thnx


Sorry for my English, it is not my first language.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60992
    
  65

See the JSP FAQ. Page-relative URLs are fragile even when you can get them to work. Use server-relative URLs that begin with the context path.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Shankar Tanikella
Ranch Hand

Joined: Jan 30, 2011
Posts: 329

I Ion,
Anywhere in in your war should be fine and if you have it well structured in a package say for example

myProjectWAR
¦
¦_ _ Images -> {your images goes here in this directory}

then you got to use the path relative to your war and it looks something like below

<img src = "Images/file.png" />
it should work


Have Fun with Java
little,little.. little by little makes a lot..
sri ramvaithiyanathan
Ranch Hand

Joined: Nov 20, 2010
Posts: 109
Popescu ,
Make sure your project structure is well formed
I'm just giving example project structure

project
|
----JSP-->index.jsp
|
----Images-->example.jpg

use this way,It's good way of practicing <%=request.getContextPath()%>/Images/example.jpg

Regards,
Sriram.V


For java examples,ebooks,interview questions,visit this blog
http://periodicupdates.blogspot.com/
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60992
    
  65

Shankar Tanikella wrote:then you got to use the path relative to your war and it looks something like below
<img src = "Images/file.png" />
it should work

No. Again, that's a page-relative link and is fragile and will cause issues either now or in the future. Please see the JSP FAQ on this issue.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60992
    
  65

sri ramvaithiyanathan wrote:It's good way of practicing <%=request.getContextPath()%>/Images/example.jpg

That would have been fine 10 years ago. In a modern JSP there should be no scriptlets and the EL should be used to fetch the cntext path with ${pageContext.request.contextPath}.

It also advised and conventional to use all lowercase for folder names.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to insert an image