I have a jar file in my WEB-INF/lib directory. Inside that jar is a META-INF/resources directory with one file (image.jpg). As I understand the servlet 3.0 API, when I deploy this as part of my web app under Tomcat 7, I should be able to go to
http://host/context/image.jpg
and see the image loaded from within the jar. but instead, I get a 404 error. It's as if the servlet_api isn't loading resources from within my jars even though the documentation says it should.
What am I doing wrong? Is there a field I need to put in my web.xml file to tell tomcat to load these resources and make them available to the web browser?
The way the jar is built is correct. Tomcat 7 ships with the Servlet 3.0 jar, but it will not serve resources out of the jar unless the web.xml specifically states that it is version 3.0. Tomcat will not assume you want Servlet 3.0 functionality.
In your web.xml, your web-app tag need to start like this:
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
Note the references to version 3.0
As soon as you specify the web-app is version 3.0, you'll gain access to Servlet 3.0 functionality.
Maybye it helps someone.
Some people make in their catalina.properties:
tomcat.util.scan.StandardJarScanFilter.jarsToSkip=*
to spped up Tomcat start time.
If I did it on my Tomcat 9, the resources in jar was for me unaccesible. I had to make exception:
tomcat.util.scan.StandardJarScanFilter.jarsToScan = libraryWithResources.jar,....
and the problem went away.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With