Skip to main content

Issue with Servlet 3.0 resource handling

2 replies [Last post]
emailnbw
Offline
Joined: 2008-05-28
Points: 0

I was experimenting with packaging resources in the META-INF/resources directory of a JAR which is then packaged into my WARs WEB-INF/lib directory and came across some odd behavior.

The Servlet 3.0 specification states that static content can be packaged in that way and will be delivered as such. The issue I am having with this is when I package content in a sub directory it is not found.

For example, if my JAR looks like so:

my-webjar.jar
-- com
/foo
/Main.class
-- META-INF
/resources
/index.html

and I package that JAR into a WAR like so:

my-webapp.war
-- WEB-INF
/lib
/my-webjar.jar

and then deploy this to Glassfish 3.1.2.2 and hit the URL http://localhost:8080/my-webapp/index.html I see the index.html that is bundled in the JAR.

However, if I change the JARs layout to place index.html in a sub directory of the resources directory like so:

my-webjar.jar
-- com
/foo
/Main.class
-- META-INF
/resources
/foo
/index.html

and deploy this and access the URL http://localhost:8080/my-webapp/foo/index.html I get a 404.

Various parts of the Servlet 3.0 spec would imply that you can create a directory structure under the resources directory and that files in that structure will be served as static resources, however, in practice I am only seeing files directly under the resources directory being served as static resources.

For simplicity sake in testing serving static resources in this way I set up my test case to not include a web.xml or a class annotated as a servlet.

Is this a factor of how the Default servlet is mapping static resources? Do I have to include a web.xml with some mappings which include a * wildcard to handle subdirectories? Have I stumbled on a legit bug?

Thanks,

-Noah

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
swchan2
Offline
Joined: 2005-03-29
Points: 0

Hi Noah,

I have the following locally:
1. my.war with the content: WEB-INF/lib/my.jar, without web.xml
2. in my.jar, we have
META-INF/resources/foo/index.html
META-INF/resources/index.html

I can access the following with the correct contents:
http://localhost:8080/my/index.html
http://localhost:8080/my/foo/index.html

Shing Wai Chan

emailnbw
Offline
Joined: 2008-05-28
Points: 0

Hi Shing,

Thanks for your reply. I rebuilt the project and restarted the server and now the index.html resource is being delivered as expected in either case. I'm not sure what was going on before. Sorry for a false alarm.

-Noah

On Nov 5, 2012, at 6:48 PM, Shing Wai Chan wrote:

> Hi Noah,
>
> I have the following locally:
> 1. my.war with the content: WEB-INF/lib/my.jar, without web.xml
> 2. in my.jar, we have
> META-INF/resources/foo/index.html
> META-INF/resources/index.html
>
> I can access the following with the correct contents:
> http://localhost:8080/my/index.html
> http://localhost:8080/my/foo/index.html
>
> Shing Wai Chan