Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

how to insert an image

 
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
 
Marshal
Posts: 67289
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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.
 
Ranch Hand
Posts: 329
Eclipse IDE Oracle Java
  • 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
 
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
Marshal
Posts: 67289
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
Marshal
Posts: 67289
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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.
 
This looks like a job for .... legal tender! It says so right in this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!