Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 576
The Masked Crusader Avatar asked Jun 26 '12 21:06

The Masked Crusader


2 Answers

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.

like image 194
The Masked Crusader Avatar answered Jan 02 '23 23:01

The Masked Crusader


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.

like image 38
Sergiusz Brzeziński Avatar answered Jan 03 '23 00:01

Sergiusz Brzeziński