• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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: 67752
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: 67752
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: 67752
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.
 
Lasagna is spaghetti flvored cake. Just like this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic