0

I'm trying to get some pics from disc I had put the pic as

D:
|
|-images
       |
       |-attachments
                ->testimg.jpg

and I'm trying to get this pics using a context file in META-INF on the webapp here is the code

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

in the Java code to set the img src attribut I do

pics.setSrc("/attachments/testimg.jpg");

but the problem is when I run the page in the browser motion that the ressource isn't found and when I inspect the network I found the type is text/html enter image description here enter image description here I hope you could help me

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user3260388
  • 313
  • 1
  • 4
  • 10

1 Answers1

1

Try removing the forward slash from the end of your path. So modify path="/Projet/attachments/" to path="/Projet/attachments" I've replicated it locally and it solved the problem.

The reason why you get text/html type back when trying to retrieve a resource that doesn't exist is because the webserver (in our case tomcat) generates a 404 error page, and that is of type text/html. Just try manually hitting http://localhost:8080/path/that/does/not/exist.jpg you should get the same result.


Further info

If you want to set it up with a clean install, no Java code needed to test the mapping

  • Deploy tomcat
  • Edit conf/server.xml
<Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true">
   .
   .
   <Context  docBase="/space/images/attachments" path="/Projet/attachments" reloadable="true" />
</Host>
  • (Re)start tomcat
  • Hit http://localhost:8080/Projet/attachments/testimg.jpg in browser
chiswicked
  • 873
  • 9
  • 9
  • I tested it ,it's not working ,I don't know if the context file is working or not ,where should I put this lines in the server.xml in eclipse or in conf/server.xml or in META-INF/context.xml of the webapp? – user3260388 Jun 01 '14 at 16:23
  • For testing it's easiest to change it in `conf/server.xml`. Put your `Context` node inside the `Host`. I'm saying it's easiest as there aren't any overriding rules so it definitely takes effect. – chiswicked Jun 01 '14 at 17:26