resourceshttp-status-code-404

resources in META-INF/resources not found and return 404


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?


Solution

  • 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.