• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

how to insert an image

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67645
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 329
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67645
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67645
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic