2

I am trying to include an image (my company logo) in an HTML page. When my HTML is not hosted on tomcat and I give absolute file path in HTML, everything works fine and I am able to see the image

The following works perfectly fine outside tomcat:

<footer>
    <br/><br/><br/><br/><br/><br/><br/>
  <center><p>Powered by: </p>
  <img src="D:\MyImagesFolder\logo.png"  /></center>
</footer>

BUT. When I access the same HTML from localhost:8080/myform, the image does not load

I have tried the following

  1. Added context.xml in my web project's META-INF and added the following to it

    <?xml version="1.0" encoding="UTF-8"?>
    <Context path="/MyContext/images" docBase="D:\MyImagesFolder" crossContext="true"/>

AND in the HTML

  <img src="MyContext/images/logo.png"  />

then added a leading slash to make it

<img src="/MyContext/images/logo.png"  />

No LUCK

Then I tried the following

  1. Under apache-tomcat-9.0.0.M21\conf I created a folder Catalina and a folder localhost inside Catalina

Created an XML file named MyContext#images.xml in localhost which was having the following

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/MyContext/images" docBase="D:\MyImagesFolder" crossContext="true" />

and in the HTML I tried

<img src="MyContext/images/logo.png"  />

and

<img src="/MyContext/images/logo.png"  />

NO LUCK

  1. I put my image inside \apache-tomcat-9.0.0.M21\webapps\ROOT and in the html

NO LUCK

  1. As I am running tomcat inside eclipse, I tried putting MyContext#images.xml in "Tomcat v9.0 Server at localhost-config" in eclipse and

    <?xml version="1.0" encoding="UTF-8"?>
    <Context path="/MyContext/images" docBase="D:\MyImagesFolder" crossContext="true"/>

in the XML

and in the HTML I tried

<img src="MyContext/images/logo.png"  />

and

<img src="/MyContext/images/logo.png"  />

NO LUCK

What is missing and where?

try-catch-finally
  • 7,436
  • 6
  • 46
  • 67
Nostalgic
  • 300
  • 1
  • 4
  • 18

1 Answers1

2

I found the solution here - How to serve static files in my web application on Tomcat

Simple created a folder WebContent/images , placed my png file there and in teh html

<img src="images/gitc_logo.png"  />

And now it works..

Nostalgic
  • 300
  • 1
  • 4
  • 18