Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to insert an image

 
Popescu Ion
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shankar Tanikella
Ranch Hand
Posts: 329
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
sri ramvaithiyanathan
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic